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

后端 未结 9 904
灰色年华
灰色年华 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 11:51

    After much struggle, I was finally able to solve this.

    1. Change TableViewController to UIViewController
    2. Drag a TableView to the UIViewController and align it right underneath the navigation bar in the main.storyboard
    3. Add missing constraints.
    4. click on tableview and inspect the constraints
    5. set bottom space to: superview to 0
    6. set trailing space to superview to 0
    7. set leading space to superview to 0
    8. set "Align Top to: Top Layout Guide.Top to 0 (Very Important)

    Add the following code to the UIViewController's viewDidLoad function:

    // Create a solid color background for the status bar (in Swift Code)

    let statusFrame = CGRectMake(0.0, 0, self.view.bounds.size.width,
    UIApplication.sharedApplication().statusBarFrame.size.height)

    var statusBar = UIView(frame: statusFrame)

    statusBar.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(statusBar)

    What you are doing is creating a solid bar right beneath the navigation bar. As the navigation bar moves up, the solid bar move up too and right behind the status bar.

    The only problem is that when you rotate the screen side ways, the white bar still there even though the status bar disappears.

提交回复
热议问题