8
votes

I have a stack of three VCs that are coordinated through a navigation controller and make use of a navigation bar. On this navigation bar, in each VC, I added the same button (a 'Logout' button) as a right bar button item:

UIBarButtonItem *logoutButton = 
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"LogoutButtonTitle", @"")
                                     style:UIBarButtonItemStyleBordered 
                                     target:self 
                                     action:@selector(logout)];

[categoriesViewController.navigationItem setRightBarButtonItem:logoutButton];
[[self navigationController] pushViewController:categoriesViewController animated:YES];

So each view controller has this logout button in the navigation bar. When I push any of these controllers onto the navigation stack, the new view controllers loads as expected, everything is displayed and functions correctly, but right after the new view controller is displayed (after the animation finishes) the logout button shifts about 3px to the right.

I've tried adding the button before pushing the VC, in the init methods of the VC, in the viewDidLoad and viewDidAppear but still the same behavior. I can't seem to get rid of this shift.

What's strange is that the back button disappears and reappears when pushing the new VC (using the default animation) but the 'Logout' button seems to stay in place and just shift to the right after the animation finishes. Also, the font on the buttons is not the default font but one of the system fonts (Helvetica light), may be related to this.

Any ideas on why this is happening?

2
It's because I'm not using the default font. Somehow, when rendering the button with a different font to the screen, its frame needs to be recomputed - or this is my best guess.hbot
I am having this same problem -- did you end up fixing it?Zachary Vance

2 Answers

1
votes

I had the same problem. It is because of your rootviewcontroller not being set. Set the main page or the first page where your first navigation bar would be originating as rootviewcontroller. From there [self.navcontroller] pushviewcontroller...].

0
votes

Try calling

self.navigationItem.rightBarButtonItem = yourUIBarButtonItem;

Also what is your logout button, is it the correct class? Perhaps a little more code?