Height did not update when switch from portrait to Landscape?

209 views Asked by At

I create a UIScrollView, added other data in it, added subview to self.view. The problem is that when i switch my phone from portrait to landscape or vice versa it do not update the height so my scrollview do not work as i need to. here is the code:

UIScrollView *menuScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 44+28+8, 130, self.view.frame.size.height)];
2

There are 2 answers

2
kaar3k On BEST ANSWER

Override didRotateFromInterfaceOrientation method on your ViewController and change the frame of the scrollView.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    self.menuScrollView.frame=CGRectMake(0,yPosition,width,self.view.frame.size.height);
}
0
Gabb On

The scroll view doesn't change its size because you haven't set any constraints at all.

There are two ways to do that. The first and really easy one, is to create both the scroll view and its subviews in Interface Builder, and set all the necessary constraints there. The second, harder way, is to programmatically set them, but if you're uncomfortable in doing that, then you should stick to the first option and do in your storyboard (or in a nib file) all the work.

Personally, I'd go with the first way, and I would create IBOutlet properties so I programmatically set any other properties of the subviews later in code.