自动调整遮罩和全屏视图

Jer*_*emy 3 ios autoresizingmask

我正在尝试一些奇怪的东西.我想创建一个适合我所有屏幕的视图(减去statusBar减去navBar).这就是我在我的内心所做的viewDidLoad:

CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-20-navHeight)];
test.backgroundColor = [UIColor greenColor];
//test.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:test];
Run Code Online (Sandbox Code Playgroud)

如果我留下自动调整行注释,这可以正常工作,但是当我旋转时,它不再适合(这是预期的).

如果我取消注释自动调整行,当我第一次以纵向模式启动时,我的视图大小就像320x430,当我第一次以横向模式启动时,它就像480x200 ......"首次启动"我的意思是"来自另一个视图".

我尝试添加灵活的边距,但它没有修复(无论如何我希望它是全屏的,所以(x,y)将永远是(0,0).

Dmi*_*nko 6

UINavigationController将为您调整其子视图的大小,因此您不必考虑导航或状态栏.您只需要将此视图添加到VC的视图并将其设置为灵活的宽度/高度,以便它将填充父视图.

    UIView *test = [[UIView alloc] initWithFrame:self.view.bounds];
    test.backgroundColor = [UIColor greenColor];
    test.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:test];
Run Code Online (Sandbox Code Playgroud)