topLayoutGuide in child view controller

前端 未结 11 2095
既然无缘
既然无缘 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:14

    While this answer might be correct, I still found myself having to travel the containment tree up to find the right parent view controller and get what you describe as the "real topLayoutGuide". This way I can manually implement automaticallyAdjustsScrollViewInsets.

    This is how I'm doing it:

    In my table view controller (a subclass of UIViewController actually), I have this:

    - (void)viewWillLayoutSubviews {
        [super viewWillLayoutSubviews];
    
        _tableView.frame = self.view.bounds;
    
        const UIEdgeInsets insets = (self.automaticallyAdjustsScrollViewInsets) ? UIEdgeInsetsMake(self.ms_navigationBarTopLayoutGuide.length,
                                                                                                   0.0,
                                                                                                   self.ms_navigationBarBottomLayoutGuide.length,
                                                                                                   0.0) : UIEdgeInsetsZero;
        _tableView.contentInset = _tableView.scrollIndicatorInsets = insets;
    }
    

    Notice the category methods in UIViewController, this is how I implemented them:

    @implementation UIViewController (MSLayoutSupport)
    
    - (id)ms_navigationBarTopLayoutGuide {
        if (self.parentViewController &&
            ![self.parentViewController isKindOfClass:UINavigationController.class]) {
            return self.parentViewController.ms_navigationBarTopLayoutGuide;
        } else {
            return self.topLayoutGuide;
        }
    }
    
    - (id)ms_navigationBarBottomLayoutGuide {
        if (self.parentViewController &&
            ![self.parentViewController isKindOfClass:UINavigationController.class]) {
            return self.parentViewController.ms_navigationBarBottomLayoutGuide;
        } else {
            return self.bottomLayoutGuide;
        }
    }
    
    @end
    

    Hope this helps :)

提交回复
热议问题