Search code examples
iphoneios4interface-builderuibarbuttonitemstyle

Change a UIBarButtonItem's style to a BarButtonSystemItem


I'm new to iPhone development, so I'm not sure if it is a very common issue. I didn't find anything about this on Google.

I have a UIBarButtonItem defined with Interface Builder:

@property (weak, nonatomic) IBOutlet UIBarButtonItem *cameraButton;

For now, it's just a simple button, but I want to put a camera image on it, using UIBarButtonSystemItemCamera. I know it's possible to do it with initWithBarButtonSystemItem, but I there is no method like changeWithBarButtonSystemItem. I'd like to something like:

- (void)viewDidLoad
{
  [super viewDidLoad];

  [cameraButton changeWithBarButtonSystemItem:UIBarButtonSystemItemCamera];
}

How is it possible ? Is there a way to tell Interface Builder to instantiate directly the button with this camera icon ?


Edit

It seems that it's not possible to change a button style after its instantiation. So only one question remains: Is there a way to tell Interface Builder to instantiate a button with UIBarButtonSystemItemCamera ?


Solution

  • In Interface Builder, change the UIBarButtonItem's "Identifier" to "Camera."

    If you need to change buttons at run-time, the easiest thing is to swap out buttons themselves, not try to mutate them.