RubyMotion iOS 5 Appearance API

iOS 5 introduced the appearance API so that developers could customize the appearance of certain UI elements throughout the entire application without subclassing those controls.

For example, to change the appearance of a UINavigationBar you can set the appearance in the app delegate with the following code:

navigationBar = UINavigationBar.appearance
navigationBar.setBackgroundImage(UIImage.imageNamed('navigation-bar-background.png'), forBarMetrics: UIBarMetricsDefault)
navigationBar.setTitleTextAttributes({
  UITextAttributeFont => UIFont.fontWithName('Trebuchet MS', size:24),
  UITextAttributeTextShadowColor => UIColor.clearColor,
  UITextAttributeTextColor => '#2a3284'.to_color
})

I had to use the hashrocket => instead of the new colon notation since using the colon notation resulted in the key constants being turned into symbols.

The .to_color helper is part of the BubbleWrap library. I’ll talk about that library in a separate post.

Leave a comment

name*

email* (not published)

website