Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar appearance in RubyMotion not working

I'm trying to customize the navigation bar in a RubyMotion app but can't seem to change the title text font or color. I can set the background image and tint but not the title attributes.

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

The console output shows this for the inspect statement:

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

So it appears that everything is being set correctly but all I get is the standard blue navigation bar with white text.

like image 671
JWright Avatar asked Dec 06 '22 11:12

JWright


1 Answers

UITextAttributeFont, etc. are actual constants, but when you use them in a newer Ruby 1.9-style hash assignment, it's converting them to symbols.

The easiest way around this is to use the older hash-rocket assignment style (make sure to not put colons in front of the UIText... items):

navigationBar.setTitleTextAttributes({
  UITextAttributeFont => UIFont.fontWithName('Trebuchet MS', size:24),
  UITextAttributeTextShadowColor => UIColor.colorWithWhite(0.0, alpha:0.4),
  UITextAttributeTextColor => UIColor.blueColor
})
like image 63
Dylan Markow Avatar answered Jan 09 '23 08:01

Dylan Markow