如何在导航栏中设置背景图像

问题描述:

我正在使用最新版本的RubyMotion。如何在导航栏中设置背景图像

我想在RubyMotion中的导航栏上设置自定义背景图像。但我不断收到错误。

这是在AppDelegate中我的代码:

dashboardController = DashboardController.alloc.initWithNibName(nil, bundle: nil) 
nav_controller = UINavigationController.alloc.initWithRootViewController(dashboardController) 

image = UIImage.imageNamed('navbar/background_navbar.png') 
nav_controller.appearance.setBackgroundImage(image, forBarMetrics:UIBarMetricsDefault) 

这是错误:

*** Terminating app due to uncaught exception 'NoMethodError', reason: 'app_delegate.rb:10:in `application:didFinishLaunchingWithOptions:': undefined method `appearance' for #<DashboardController:0x6c4a9a0> (NoMethodError) 

唯一的例外是告诉你,UINavigationController不具有appearance方法。

如果要更改应用中所有导航栏的外观,可以拨打UINavigationBar代理UINavigationBar.appearance.setBackgroundImage(...

如果你想改变只是当前UINavigationControllernavigationBar然后调用nav_controller.navigationBar.setBackgroundImage(...

这里有一个很好的例子:https://github.com/IconoclastLabs/rubymotion_cookbook/tree/master/ch_2/10_imagenavbar

代码究竟做你想要什么。

+0

不,不幸的不是。我只是将图像添加到导航栏中,而不是设置导航栏背景图像。下面的答案虽然解决了:) –