iOS 11 on NavigationBar pull down - height of bar

2019-06-13 20:04发布

问题:

What I want to to: I want to drag down the whole view of a viewController to dismiss to the parent viewController using a pan gesture recognizer.

The Problem: When I drag the view down, the navigationBar decreases its height and does not look good. When the view returns to its original position, the navigationBar returns to the default size. I want the navigationBar to stay at its size. I also tried to use the new large titles and some other properties of the navigationController/-bar, but that did not solve it. Note: Everything worked fine already before iOS 11.

My code:

override func viewDidLoad() {
    super.viewDidLoad()
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(dragViewDown(_:)))
    navigationController!.view.addGestureRecognizer(panGesture)
}

@IBAction func dragViewDown(_ gesture: UIPanGestureRecognizer) {
    if let dragView = gesture.view {
        let translation = gesture.translation(in: dragView)

        dragView.center.y = (dragView.center.y + translation.y)

        gesture.setTranslation(CGPoint.zero, in: dragView)
    }
}

This test project only has one viewController and does not provide the dismissal, but the problem is the same as in my working project.

I also uploaded the project to GitHub: https://github.com/maddinK7/navitationBar-pull-down-problem

Does anyone have an idea how to solve this? Thanks in advance.

回答1:

I want the navigationBar to stay at its size

It is staying at its size. If you check the navigation bar's bounds size height before, during, and after the drag, you will see that it remains the same (probably 44) at all times. What's changing is the drawing extension that causes the drawing of the nav bar to extend up behind the status bar. It can't do that when you pull the whole thing away from the top of the screen, because it is not at the top next to the status bar any more. iOS 11 is more strict about the way it performs this drawing extension, probably because it has to do it in a special way on the iPhone X.

So, let's make sure you're doing this correctly:

  • Make sure that the navigation bar has a top constraint pinned to the safe area layout guide's top, with a constant of zero.

  • Make sure that the navigation bar has a delegate that returns .topAttached from position(forBar:).

If you are doing both those things and it doesn't help, you'll have to implement this in some other way entirely. Making the view directly draggable like this, without a custom parent view controller, was always dubious.



回答2:

When UINavigationController attached top, system will add safe area top margin in the navigation background.

(NOTICE: Background margin will not changed when offset value is between 1 and 0)

So you have to handle attached/detached top event by handle gesture offset to change the right offset and content insets.

You can try the solution in my lib example. ;) My example include UITableViewController in the UINavigationController, so it will relatively complex.

https://github.com/showang/OverlayModalViewController