0
votes

I'm trying to create a custom navigation bar with title and two custom baritems. What I've done so far:
1. subclass a UINavigationBar
2. override -(void) drawRect:(CGRect)rect method, to draw image as a background

Now I get stuck when making right bar button item.
In my CustomBar class I've overriden pushNavigationItem method as follows:

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated {
     [super pushNavigationItem:item animated:NO];
     item.rightBarButtonItem.customView.frame = CGRectMake(250, 5, 60,30); 
}

and in my view controller I've done something like:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationController.navigationItem.rightBarButtonItem = barButton;
[self.navigationController.navigationBar pushNavigationItem:self.navigationItem animated:NO]

but I always get SIGABRT when calling

[super pushNavigationItem:item animated:NO];

If I leave that line out, everything runs fine, but there is no button in the navigation bar.

What is the proper way to add custom bar button item to custom navigation bar?

EDIT: For clarification, I've added an image.

This is what I need. enter image description here

I'm trying to create the logout button on the right

1
You don't actually show how you're instantiating your CustomBar class. Are you subclassing UINavigationController? - smparkes
I am subclassing UINavigationBar. I make UINavigationController in a nib, set its navigation bar to my CustomNavBar class and load the controller from a nib. - Maggie

1 Answers

0
votes

Have you tried removingnavigationController while setting the item ? I used to do that for setting the title, never used to get the title.

Try [[self navigationItem] setRightBarButtonItem:yourButton];

In your case :

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = barButton;`