3

我正在尝试一些奇怪的东西。我想创建一个适合我所有屏幕的视图(减去状态栏减去导航栏)。这是我在我的内部所做的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];

如果我留下 autoresizing 行的注释,这工作正常,但是当我旋转时,它不再适合(这是预期的)。

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

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

4

1 回答 1

6

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

    UIView *test = [[UIView alloc] initWithFrame:self.view.bounds];
    test.backgroundColor = [UIColor greenColor];
    test.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:test];
于 2013-02-18T23:48:37.647 回答