Posts tagged "iPhone"

April 10, 2009
January 22, 2009

iPhone Protip: Fake navbar buttons

If you need to have a third button in your navigation bar, you’ve probably discovered already that you can’t add a UIBarButtonItem as you only have 2 slots for those, on the left and the right. There’s the titleView property, but you need to provide your own view and UIButton doesn’t have a style for bar buttons.

However, there is a reasonably easy solution. UISegmentedControl has a bar style that looks exactly like one of the buttons. The only problem is it doesn’t behave like a button. However, you can make a simple subclass that overrides the -touchesBegan:withEvent: and friends methods in order to make it behave like one after all. Now you can stick that in titleView and it looks just fine.

If you want it to be right-aligned on the bar, there’s another simple trick. Just create a UIView, set its frame to the bounds of the navbar, set your button as a subview right-aligned in the view and set the autoresizeMask to keep it there. UINavigationBar will resize the titleView to fill all the remaining space, which will mean your button is now right-aligned right next to the rightBarButtonItem.

January 20, 2009

iPhone Protip: nib top-level objects

When UIViewController loads a nib file, it does not retain all top-level objects in the nib (unlike MainWindow.nib where all top-level objects are retained). If you use top-level objects besides your view, you must make sure you implement the IBOutlets as retained properties.