Question

I've seen many of applications that have an info button (the letter "i" with a circle around it) on the uinavigationbar. How do I add this type of button?

Was it helpful?

Solution

The previous answer was close, didn't quite compile. Here's what you really want:

// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

OTHER TIPS

There is memory leak in the answer above. You should use "autorelease" to avoid it. I have tried to edit the answer but I wasn't successful so far.

// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];

THIS IS THE CORRECT WAY TO DO IT.

You should be able to add an "Info Light" type button to the navigationItem of the view controller that the UINavigationController is currently displaying. Something like this could go in your view controller code:

- (void) viewDidLoad {
  UIButton * info = [UIButton buttonWithType:UIButtonTypeInfoLight];
  // set your button's target and selector for whatever action here.
  self.navigationItem.rightBarButtonItem.customView = info;
}

You could also use your current bar button item's target and selector for your info button.

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self.barButtonItem.target action:self.barButtonItem.action forControlEvents:UIControlEventTouchUpInside];
self.barButtonItem.customView = infoButton;

This way, any actions or segues coupled to your bar button item will be transferred to your info button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top