简体   繁体   English

自动调整遮罩和全屏视图

[英]Autoresizing mask and full-screen view

I'm experimenting something odd. 我正在尝试一些奇怪的东西。 I want to create a view which will fit all my screen (minus statusBar minus navBar). 我想创建一个适合我所有屏幕的视图(减去statusBar减去navBar)。 Here's what I've done inside my viewDidLoad : 这是我在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];

If i leave the autoresizing line commented, this works fine, but when I rotate, it does not fit anymore (that's expected). 如果我留下自动调整行注释,这可以正常工作,但是当我旋转时,它不再适合(这是预期的)。

If I uncomment the autoresizing line, when I first launch in portrait mode my view's size is something like 320x430 and when I first launch in landscape mode it's like 480x200... With "first launch" I mean "come from another view". 如果我取消注释自动调整行,当我第一次以纵向模式启动时,我的视图大小就像320x430,当我第一次以横向模式启动时,它就像480x200 ......“首次启动”我的意思是“来自另一个视图”。

I tried adding flexible margins but it didn't fix (anyway I want it to be full-screen, so (x,y) will always be (0,0). 我尝试添加灵活的边距,但它没有修复(无论如何我希望它是全屏的,所以(x,y)将永远是(0,0)。

UINavigationController will resize it's child views for you, so you don't have to account for navigation or status bar. UINavigationController将为您调整其子视图的大小,因此您不必考虑导航或状态栏。 You only really need to add this view to VC's view and set it to flexible width/height so it'll fill parent view. 您只需要将此视图添加到VC的视图并将其设置为灵活的宽度/高度,以便它将填充父视图。

    UIView *test = [[UIView alloc] initWithFrame:self.view.bounds];
    test.backgroundColor = [UIColor greenColor];
    test.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:test];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM