JRoss
JRoss

Reputation: 177

RightBarButtonItem Does Not Appear in Navigation Bar

This is bugging the heck out of me! I'm just trying to programmatically add a right-side "flash" button to my navigation bar, and for whatever reason, I can't make it appear. My code is below. Can anyone help me? Thanks in advance.

 //Note: flashButton is declared as a UIButton property in .h, and synthesized in .m        
    UIView *flashContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];

    [flashButton addTarget:self action:@selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
    [flashButton setBackgroundImage: [UIImage imageNamed:@"FlashIconInactive_small.png"] forState:UIControlStateNormal];
    [flashButton setBackgroundImage: [UIImage imageNamed:@"FlashIconActive_small.png"] forState:UIControlStateHighlighted];

    [flashContainer addSubview:flashButton];
    [flashButton sizeToFit];

    UIBarButtonItem *flashButtonItem = [[UIBarButtonItem alloc] initWithCustomView:flashContainer];

    scannerVC.delegate = self;
    scannerVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTapped)];
    [[scannerVC navigationItem] setRightBarButtonItem:flashButtonItem];

Upvotes: 0

Views: 1066

Answers (1)

Kris Markel
Kris Markel

Reputation: 12112

Is flashButton connected to a button in a xib or storyboard via an IBOutlet? If not, you'll need to create an instance of UIButton using +[UIButton buttonWithType:].

Another test to try is to set a breakpoint on your first line (the addTarget: one) and enter po flashButton in the debugger. What's the output? If it's nil, then your button doesn't exist and needs to be created.

Upvotes: 1

Related Questions