Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios navigation bar right button item shifts when pushing new view controller

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?

like image 252
hbot Avatar asked Aug 01 '12 15:08

hbot


1 Answers

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...].

like image 159
Ashwin G Avatar answered Nov 05 '22 22:11

Ashwin G