How to prevent status bar from overlapping content with hidesBarsOnSwipe set on UINavigationController?

后端 未结 9 864
灰色年华
灰色年华 2020-12-02 11:10

I\'m trying to use the new feature added in iOS 8 - hiding the navigation bar while user is scrolling the table view (similar to what mobile Safari does). I\'m setting the p

相关标签:
9条回答
  • 2020-12-02 12:06

    If you want to hide status bar with animation:

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return .Slide
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return navigationController?.navigationBarHidden ?? false
    }
    
    0 讨论(0)
  • 2020-12-02 12:07

    Another way to do it is just add another view (above the tableview or collectionview or webview or scrollview or whatever) and set the view's top constraint to "Superview.Top" and its bottom constraint to "Top Layout Guide.Bottom" ,set the view's background color and thats it , you can even do it all in Interface Builder without any code. And if you want to respond to that event you can add a keypath observer to the view's bounds change , or subclass the view and override its bounds setter...

    0 讨论(0)
  • 2020-12-02 12:11

    Building off of anas' answer, I have a working solution (I'm assuming tableViewController is your UITableViewController instance):

    In a UINavigationController subclass (or also potentially from tableViewController):

    - (void)viewDidLoad {
        if ([self respondsToSelector:@selector(barHideOnSwipeGestureRecognizer)]) {
            // iOS 8+
            self.hidesBarsOnSwipe = YES;
            [self.barHideOnSwipeGestureRecognizer addTarget:self action:@selector(swipe:)];
        }
    }
    
    - (void)swipe:(UISwipeGestureRecognizer *)recognizer {
        BOOL shouldHideStatusBar = self.navigationController.navigationBar.frame.origin.y < 0;
        tableViewController.hideStatusBar = shouldHideStatusBar;
        [UIView animateWithDuration:0.2 animations:^{
            [tableViewController setNeedsStatusBarAppearanceUpdate];
        }];
    }
    

    In your tableViewController:

    @property(nonatomic, getter = shouldHideStatusBar) BOOL hideStatusBar;
    
    - (BOOL)prefersStatusBarHidden {
        return [self shouldHideStatusBar];
    }
    

    Let me know if this doesn't work for you. A few non-obvious things:

    • self.navigationController.navigationBar.frame.origin.y was -44 (the negative height of the navigation bar) when hidden, and 20 (the height of the status bar) when visible. There was no in-between, even during animations, so a negative value == hidden and a nonnegative value == visible.
    • The child view controller is the one queried for whether or not the status bar should be hidden. In my case, I have a UIViewController within a UINavigationController within a UITabBarController, and it didn't work until I overrode prefersStatusBarHidden on the UIViewController.
    • Since a hidden status bar has no frame, your content might jerk upwards 20 points unless you wrap the call to setNeedsStatusBarAppearanceUpdate in an animation block.
    • Hopefully the syntax is correct; I backported this from my Swift code.
    0 讨论(0)
提交回复
热议问题