问题描述
我有一个带有半透明状态栏和导航栏的 UIPageViewController
.正如预期的那样,它的 topLayoutGuide
是 64 像素.
I have a UIPageViewController
with translucent status bar and navigation bar. Its topLayoutGuide
is 64 pixels, as expected.
然而,UIPageViewController
的子视图控制器报告 topLayoutGuide
为 0 像素,即使它们显示在状态栏和导航栏下.
However, the child view controllers of the UIPageViewController
report a topLayoutGuide
of 0 pixels, even if they're shown under the status bar and navigation bar.
这是预期的行为吗?如果是这样,在真正的 topLayoutGuide
下定位子视图控制器的视图的最佳方法是什么?
Is this the expected behavior? If so, what's the best way to position a view of a child view controller under the real topLayoutGuide
?
(缺少使用 parentViewController.topLayoutGuide
,我认为这是一个 hack)
(short of using parentViewController.topLayoutGuide
, which I'd consider a hack)
推荐答案
虽然 这个答案 可能是正确的,但我仍然发现自己必须遍历容器树才能找到正确的父视图控制器并获得您所描述的真正的 topLayoutGuide
".这样我就可以手动实现automaticallyAdjustsScrollViewInsets
.
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
.
我是这样做的:
在我的表视图控制器(实际上是 UIViewController
的子类)中,我有这个:
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;
}
注意UIViewController
中的category方法,我是这样实现的:
Notice the category methods in UIViewController
, this is how I implemented them:
@implementation UIViewController (MSLayoutSupport)
- (id<UILayoutSupport>)ms_navigationBarTopLayoutGuide {
if (self.parentViewController &&
![self.parentViewController isKindOfClass:UINavigationController.class]) {
return self.parentViewController.ms_navigationBarTopLayoutGuide;
} else {
return self.topLayoutGuide;
}
}
- (id<UILayoutSupport>)ms_navigationBarBottomLayoutGuide {
if (self.parentViewController &&
![self.parentViewController isKindOfClass:UINavigationController.class]) {
return self.parentViewController.ms_navigationBarBottomLayoutGuide;
} else {
return self.bottomLayoutGuide;
}
}
@end
希望这有帮助:)
这篇关于子视图控制器中的 topLayoutGuide的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!