marciokoko
marciokoko

Reputation: 4986

Why does my UIBarButtonItem not appear on top right corner?

I have this UIViewController with this code in the .m:

@property (nonatomic,strong) IBOutlet UINavigationBar *navBar;
@property (nonatomic,strong) IBOutlet UIBarButtonItem *button;

@end

@implementation DetailViewController

    - (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIBarButtonItem *listoButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ListoBtn1.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(done:)];

    self.navBar.topItem.rightBarButtonItem = listoButton;

    //self.navBar.topItem.backBarButtonItem = self.button;
    [self setVariables];

}

The button appears but it looks like this:

barbuttonitem

I just want my image to appear, not that back button. How do I remove its border?

Upvotes: 0

Views: 570

Answers (2)

Chris Tetreault
Chris Tetreault

Reputation: 1973

To remove the border create your UIBarButtonItem with a UIButton customView

UIButton *listoButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *listoButtonImage = [UIImage imageNamed:@"ListoBtn1.png"];
[listoButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
[listoButton setBackgroundImage:listoButtonImage forState:UIControlStateNormal];
listoButton.frame = CGRectMake(0, 0, listoButtonImage.size.width, listoButtonImage.size.height);
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:listoButton];
self.navBar.topItem.rightBarButtonItem = barButton;

Upvotes: 1

Wain
Wain

Reputation: 119031

First, the NIB hasn't completed loading when you create the button in initWithNibName and second your UINavigationBar doesn't automatically use the navigationItem (it's automatically used by the navigation controller). If you want to add the button to your navBar you should do it in viewDidLoad.

Upvotes: 5

Related Questions