Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationItem Prompt Issue

I'm having a problem with the prompt on a UINavigationItem that I just can't resolve...

I have a master and a detail view controller. When I push from the master to the detail a prompt is shown on the detail view controller:

prompt

However, when I pop back to the master view controller, the view isn't resized and the window shows through (the window has been coloured red):

window

This only happens on iOS7, on iOS6 the view resizes as expected.

I've tried a few things such as setting the prompt to nil in viewWillDisappear or viewDidDisappear but nothing seems to fix it.

If I set the navigation bar in the navigation controller to translucent it does fix this - unfortunately that's not an option.

I've created a very small example project here which demonstrates the issue: https://github.com/InsertWittyName/NavigationItemPrompt

Thanks in advance for any help!

like image 203
InsertWittyName Avatar asked Mar 01 '14 14:03

InsertWittyName


People also ask

What is uinavigationitem?

The items that a navigation bar displays when the associated view controller is visible. When building a navigation interface, each view controller that you push onto the navigation stack must have a UINavigationItem object that contains the buttons and views you want to display in the navigation bar.

How do I use navigation items in uinavigationcontroller?

The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content. A navigation item always reflects information about its associated view controller. The navigation item must provide a title to display when the view controller is topmost on the navigation stack.

What information does a navigation item always reflect?

A navigation item always reflects information about the view controller with which it is associated. The navigation item must provide a title to display when the view controller is topmost on the navigation stack.


3 Answers

A solution I can think of is to subclass the UIView of the master, and implement viewDidMoveToSuperview to set the frame of the view to be from the navigation bar's height to the end of the superview. Since the navigation bar is not translucent, your job is easier, as you don't have to take into account layout guides and content insets.

A few things to notice. When pushing and popping, the system moves your view controller's view into another superview for the animation and then returns it to the navigation controller's private view hierarchy. Also, when a view goes outside of the view hierarchy, the superview becomes nil.

Here is an example implementation:

@interface LNView : UIView

@end

@implementation LNView

- (void)viewDidMoveToSuperview
{
    [super viewDidMoveToSuperview];

    if(self.superview != nil)
    {
        CGRect rect = self.superview.bounds;

        rect.origin.y += 44;
        rect.size.height -= 44;

        [self setFrame:rect];
    }
}

@end

This is not a perfect implementation because it uses a hardcoded value for the navigation bar's height, does not take into account a possible toolbar, etc. But all these you can add as properties to this view and in viewDidLoad, before it starts going into the view hierarchy, set the parameters according to your needs.

like image 143
Léo Natan Avatar answered Oct 10 '22 06:10

Léo Natan


You can remove the prompt when the user taps the back button, like this

override func willMove(toParentViewController parent: UIViewController?) {
    super.willMove(toParentViewController: parent)
    if parent == nil {
        navigationItem.prompt = nil
    }
}
like image 33
Elijah Avatar answered Oct 10 '22 07:10

Elijah


The problem exists whether your nav bars are opaque or translucent. It sucks that Apple has allowed this heinous bug to plague us for over three years now.

All of these solutions are hacks. My solution is to either A) NEVER use prompts, or B) use them in every single view even if you have to set them to "".

like image 30
Scenario Avatar answered Oct 10 '22 05:10

Scenario