topLayoutGuide in child view controller

前端 未结 11 2067
既然无缘
既然无缘 2020-12-07 11:29

I have a UIPageViewController with translucent status bar and navigation bar. Its topLayoutGuide is 64 pixels, as expected.

However, the ch

11条回答
  •  死守一世寂寞
    2020-12-07 12:04

    I might be wrong, but in my opinion the behaviour is correct. The topLayout value can be used by the container view controller to layout its view's subviews.

    The reference says:

    To use a top layout guide without using constraints, obtain the guide’s position relative to the top bound of the containing view.

    In the parent, relative to the containing view, the value will be 64.

    In the child, relative to the containing view (the parent), the value will be 0.

    In the container View Controller you could use the property this way:

    - (void) viewWillLayoutSubviews {
    
        CGRect viewBounds = self.view.bounds;
        CGFloat topBarOffset = self.topLayoutGuide.length;
    
        for (UIView *view in [self.view subviews]){
            view.frame = CGRectMake(viewBounds.origin.x, viewBounds.origin.y+topBarOffset, viewBounds.size.width, viewBounds.size.height-topBarOffset);
        }
    }
    

    The Child view controller does not need to know that there are a Navigation and a Status bar : its parent will have already laid out its subviews taking that into account.

    If I create a new page based project, embed it in a navigation controller, and add this code to the parent view controllers it seems to be working fine:

    enter image description here

提交回复
热议问题