Bart Jacobs
Bart Jacobs

Reputation: 9082

Back button title truncated after restoring state navigation controller

I have run into some odd behavior of UIBarButtonItem/UINavigationBar when I attempt to restore the state of a UINavigationController between launches.

The below code snippet shows how I restore the state of a navigation controller. This code snippet is executed in the viewDidLoad method.

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([ud objectForKey:HBStateRestorationQuoteSelection]) {
    NSInteger index = [ud integerForKey:HBStateRestorationQuoteSelection];

    // Fetch Quote
    NSDictionary *quote = [self.quotes objectAtIndex:index];

    // Initialize Quote View Controller
    HBQuoteViewController *vc = [[HBQuoteViewController alloc] initWithNibName:@"HBQuoteViewController" bundle:[NSBundle mainBundle]];
    vc.quote = quote;

    // Push Quote View Controller Onto Navigation Stack
    [self.navigationController pushViewController:vc animated:NO];
}

The result is that the back button title is truncated for some reason. This behavior is not present when the navigation controller is used in a normal fashion.

Note that the truncation is not due to the size of the title in the navigation bar. As I mentioned before, the title of the back bar button is displayed normal when I don't use this state restoration logic.

I also tried putting the restoration code in the viewWillAppear method or changing the title of the back bar button by creating a custom back bar button, but none of these approaches solve the problem.

enter image description here

Upvotes: 1

Views: 1082

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

I suspect the problem is due to pushing the controller before the parent has a view showing. I believe you may have some success with the following although its a bit of work.

  • when you are going to restore state, then hide the navigation bar in the root view controller, and present the launch image instead of your normal content.

  • when the root view controller gets "viewDidAppear", then use a dispatch block on the main queue, and push other controllers, also configured to hide the navigation bar and showing the launch image.

  • when you get to the view controller that is SUPPOSE to be shown, at that point show the real view and enable showing the Navigation bar.

This is a compressed way of doing more or less what the system is doing, always having the parent view at least get to "viewDidAppear" before pushing another view.

Upvotes: 1

Related Questions