To experiment with autolayout and uiscrollview's I have been using this example
which I have edited to include 2 views in the scroll view, I have setup the autolayout constraints to position the views horizontally adjacent with their size set to fill the scroll view frame.
UIView *beeView = [[[NSBundle mainBundle] loadNibNamed:@"BeeView" owner:nil options:nil] firstObject];
beeView.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:beeView];
UIView *beeView2 = [[[NSBundle mainBundle] loadNibNamed:@"BeeView" owner:nil options:nil] firstObject];
beeView2.backgroundColor= [UIColor orangeColor];
beeView2.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:beeView2];
NSDictionary *views = @{@"beeView":beeView,@"beeView2":beeView2, @"scrollView":self.scrollView};
NSDictionary *metrics = @{@"height" : @200};
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[beeView(==scrollView)][beeView2(==beeView)]|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[beeView(==scrollView)]|" options:kNilOptions metrics:metrics views:views]];
which nicely produces what I intended.
However, if the scroll view's contentOffset
is nonzero and the device is rotated from portrait to landscape, the content offset of the scroll view is automatically set to 32px
. (see screenshot)
I have tried saving contentOffset
and setting it to this saved value when scrollViewDidEndDecelerating:
is called which works but is ugly as the scroll view scrolls to a 32px offset and then back to where I want it to be.
How do I control the scroll view's contentOffset
? Are the autolayout constraints wrong? Are there extra constraints I can add to control the contentOffset
when resizing the view?
For iOS 11.0 and greater, the following method solves the issue for me:
Where the 32px comes from? Is it related to your left and right scrollView margin?
Does it keep the wrong offset every time you change page ? If that the case, you should look at your scrollView's contentInsets values.
Otherwise, what I do to manage rotation on scrollView with paging is observing the scrollView's contentSize:
First, when you load the view, add the observer:
Then, when the contentSize value change, adjust the contentOffset:
Finally, remove the observer when you unload the scrollView:
I know this is an old question, but in case it is of use to anyone - I have created a subclass of
UIScrollView
calledLMPageView
that automatically applies the necessary layout constraints and adjusts the content offset on rotation. The class is available as part of the MarkupKit project on GitHub. Example usage (Swift):An additional example that uses markup to initialize a page view can be found here:
After referring back to a few previous posts: one & two. It seems you might find your solution following one of these steps:
~Edit~
Interface Builder : Attributes Inspector -> Uncheck Adjust Scroll View Insets in the Layout Properties.
IF Still Unsuccessful : Within the presented View Controller's
ViewWillLayoutSubviews
Method try setting each of the following UIScrollView properties:I am guessing a combination of option 1 and 2 will work, depending on how your Navigation Stack is structured.