UINavigationItem Prompt Issue

情到浓时终转凉″ 提交于 2019-11-30 20:14:51

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.

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
    }
}

You've given the answer yourself - brilliantly. It's a bug, but checking Translucent avoids the bug. Therefore the solution is to check Translucent and then compensate so that the nav bar looks the way you want.

To do so, make a small black rectangle image and include it in your project. Set the background image of the nav bar to this image. Check Translucent. Problem solved! The nav bar is now black opaque in appearance, but it no longer exhibits the bug.

Swift version:

class PromptViewSideEffect: UIView {

override func didMoveToSuperview() {
    super.didMoveToSuperview()
    if let superview: UIView  = self.superview {
        let rect: CGRect = superview.bounds
        rect.origin.y += 44
        rect.size.height -= 44
        self.frame = rect
        }
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!