11

我正在将 UIViewController 的视图添加到另一个 UIViewController 的视图中(使用新的 UIViewController 包含 API)。在将 vc 的视图添加为另一个的 subivew 之后,从顶部有一个奇怪的 20px 边距。

我记录了视图,它的来源是 0,0。但是,当我记录视图的超级视图时,它是:

<UIViewControllerWrapperView: 0x6c5e2c0; frame = (0 20; 703 748); autoresize = RM+BM; layer = <CALayer: 0x6c54190>>

我显然可以将它的框架更改为 0,0。但我想知道这样做的正确方法是什么?为什么vc的view的superview的frame是0,20?我应该修改这个还是有更好的方法来绕过这个奇怪的边距?

谢谢

4

9 回答 9

16

To fix this problem just check the box "Wants Full Screen" on the storyboard.

The problem appears because the ParentViewController is showing the navigation bar.

As apple documentation said :

If your app displays the status bar, the view shrinks so that it does not underlap the status bar. After all, if the status bar is opaque, there is no way to see or interact with the content lying underneath it. However, if your app displays a translucent status bar, you can set the value of your view controller’s wantsFullScreenLayout property to YES to allow your view to be displayed full screen. The status bar is drawn over the top of the view.

于 2013-02-28T13:17:28.290 回答
5

Found out what the problem was. Need to call addChildViewController: on self. So, here's the sequence of calls you need to make to get the containment to work properly:

[self addChildViewController:navVC];
[navVC didMoveToParentViewController:self];
[mainView addSubview:navVC.view];

self is the parent view controller. navVC is the child view controller you are adding. mainView is the view in parent view controller to which you ae going to add child view controller's view.

于 2012-06-29T06:00:48.987 回答
2

I prefer the code below, works for me.

    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
于 2013-03-13T14:55:58.497 回答
2

You need to add one line before first view controller load

- (void) viewDidLoad 
{
     self.automaticallyAdjustsScrollViewInsets = NO;
     [super viewDidLoad];
}
于 2016-05-09T11:20:11.097 回答
0

即使您尝试将状态栏下拉菜单设置为“无”,也要检查视图的大小属性。确保它仍然没有硬编码的高度值 460,或 20 的 y 原点(在 Xcode 的图形设计器中)。如果您希望此视图填充其整个父视图,请确保在属性中设置自动调整大小蒙版以固定到所有侧面(顶部、左侧、底部、右侧),并且也可能设置为拉伸。

只需查看您的调试输出,看起来自动调整大小属性可能仅设置为UIViewAutoresizingFlexibleBottomMarginand UIViewAutoresizingFlexibleRightMargin

最后,包含视图可能需要选中其Autoresize Subviews复选框。

于 2012-06-28T21:17:05.673 回答
0

Take a look at the wantsFullScreenLayout property. If set to YES the view will extend below the UIStatusBar.

于 2012-11-05T05:40:52.323 回答
0

I had the same problem, but I solved it in IOS 9 by using constraints (in interface builder). I did not use full screen because it is deprecated. Add a leading and a trailingMargin constraint and give them the Constant -20

enter image description here

于 2016-02-12T08:56:59.293 回答
-1

虽然我没有使用包含 API 的经验,但通过在 Identity Inspector 中将状态栏设置为 None(见下文),我已经解决了许多“20 岁以下”问题。

于 2012-06-28T21:02:23.577 回答
-9

Just use this viewport meta tag.

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

Putting "height=device-height" in there will cause this problem.

于 2013-07-22T21:20:21.307 回答