Question

I'm experimenting something odd. I want to create a view which will fit all my screen (minus statusBar minus navBar). Here's what I've done inside my 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".

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).

Was it helpful?

Solution

UINavigationController will resize it's child views for you, so you don't have to account for navigation or status bar. 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.

    UIView *test = [[UIView alloc] initWithFrame:self.view.bounds];
    test.backgroundColor = [UIColor greenColor];
    test.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:test];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top