views:

412

answers:

1

Has anyone had any success creating an info button (italic 'i' in a circle) in code (read: without Interface Builder), and then assigning it as the right bar button item of a navigation bar?

I've looked everywhere, and I can't even find how to create an info button in code. Any help would be greatly appreciated.

+2  A: 
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
Reena
Brilliant, thanks Reena. I've marked it as the accepted answer, but if you'd be so kind as to forgive my cheekiness, may I ask how one might bring the info button to left by five pixels or so? Adding it as the right bar button item means it gets quite crushed up against the right side of the screen.
David Foster
using 'leftBarButtonItem' property you can set at leftself.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
Reena
Sorry Reena. I do want it as the right bar button item, I just want to add a little bit of space on the right (thereby bringing the right button 5px to the left). Is that possible?
David Foster
you can do this by using 'titleview' property of 'navigationItem'.Take one uiview and add all controls you want to show on navigationbar including title and set frame of controls as you want and set this view as titleview of navigationItem.
Reena
Also, how do I add a target and action to this info button? Sorry I keep asking so many questions! :P
David Foster
Nevermind, figured it out: `[infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];`
David Foster
good.................
Reena