Content pushed down in a UIPageViewController with UINavigationController

后端 未结 15 643
暗喜
暗喜 2020-12-12 18:05

UPDATE 2

I\'ve been running and testing my app in the iOS Simulator using a 4-inch device. If I run using a 3.5-inch device the label doesn\'t jump.

15条回答
  •  遥遥无期
    2020-12-12 18:33

    I'm seeing the same issue as described by @Danny on iOS 9. I tried updating all my constraints to that they are not constrained to the margins, but it didn't fix the issue. I ended up having to adopt a hack similar to this one as follows;

    • For each content page to be displayed in the UIPageViewController, find the top-most constraint, the one between the Top of a view and the bottom of the top layout guide, and add an outlet for it to the view controller.
    • In each view controller with such an outlet, add another property for the preferred top distance. The two outlets look like this (in Swift):

      @IBOutlet weak var topGuideConstraint: NSLayoutConstraint!
      var topDistance: CGFloat!
      
    • In viewDidLoad(), set topDistance to the value assigned to the constraint in the storyboard:

      override func viewDidLoad() {
          super.viewDidLoad()
          topDistance = topGuideConstraint.constant
      }
      
    • In viewWillLayoutSubviews(), make sure the constraint has the proper value, adjusting for the height of the status bar when the topLayoutGuide.length is zero, which seems to be the case during the transition, but not once it's complete:

      override func viewWillLayoutSubviews() {
          super.viewWillLayoutSubviews()
          topGuideConstraint.constant = topDistance + (
              topLayoutGuide.length == 0
                  ? UIApplication.sharedApplication().statusBarFrame.size.height
                  : 0
          )
      }
      

    Repeat for every content view controller displayed in the UIPageViewController. Adjust the offset as appropriate if you're also displaying a UINavigation bar.

    This is an unfortunate hack, and I hate having to do it, but after many hours trying different things, I'm at least happy to have something that works so I can move on.

提交回复
热议问题