RubyMotion中的UINavigationBar外观无法正常工作

JWr*_*ght 4 objective-c ios ios5 rubymotion

我正在尝试在RubyMotion应用程序中自定义导航栏,但似乎无法更改标题文本字体或颜色.我可以设置背景图像和色调,但不能设置标题属性.

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)

    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)

    navigationBar = UINavigationBar.appearance
    #navigationBar.setBackgroundImage(UIImage.imageNamed('navigation-bar-background.png'), forBarMetrics: UIBarMetricsDefault)
    #navigationBar.setTintColor(UIColor.greenColor)
    navigationBar.setTitleTextAttributes({
      UITextAttributeFont: UIFont.fontWithName('Trebuchet MS', size:24),
      UITextAttributeTextShadowColor: UIColor.colorWithWhite(0.0, alpha:0.4),
      UITextAttributeTextColor: UIColor.blueColor
    })
    puts navigationBar.titleTextAttributes.inspect

    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(MainMenuController.alloc.init)
    @window.rootViewController.wantsFullScreenLayout = true
    @window.makeKeyAndVisible

    true
  end
end
Run Code Online (Sandbox Code Playgroud)

控制台输出显示了以下对于inspect语句:

{:UITextAttributeFont=>#<UICFFont:0x963aa70>,
 :UITextAttributeTextShadowColor=>UIColor.color(0x0, 0.0),
 :UITextAttributeTextColor=>UIColor.blueColor}
Run Code Online (Sandbox Code Playgroud)

所以看起来一切都设置正确,但我得到的只是带有白色文字的标准蓝色导航栏.

Dyl*_*kow 15

UITextAttributeFont等等是实际的常量,但是当你在更新的Ruby 1.9风格的哈希赋值中使用它们时,它会将它们转换为符号.

最简单的方法是使用旧的哈希火箭分配方式(确保不要将冒号放在UIText ...项目前面):

navigationBar.setTitleTextAttributes({
  UITextAttributeFont => UIFont.fontWithName('Trebuchet MS', size:24),
  UITextAttributeTextShadowColor => UIColor.colorWithWhite(0.0, alpha:0.4),
  UITextAttributeTextColor => UIColor.blueColor
})
Run Code Online (Sandbox Code Playgroud)