HOWTO: UIScrollView scroll to bottom programmatically

The autolayout function of a view controller is very useful. The UI elements can find their position after a rotation automatically. Except for UIScollerview...Orz

The reason is: The Layout in Storyboard is normally designed for portrait, not for landscape. So if an user rotates his iOS devices, the UIScrollView cannot resize its content height automatically. So a programmatical resize is needed. BUT, there is another way: just scroll the UIScrollView to the bottom(or where you want), the content will be presented in landscape correctly like in portait.

Here is the solution: Adding the following code to the view controller where the scroll view is.

-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// Scroll to the bottom
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];

via StackOverflow

No comments: