自动布局正在发生变化UIScrollView的contentOffset轮换 [英] Autolayout is changing UIScrollView's contentOffset on rotation
问题描述
要使用自动布局和UIScrollView的我一直在使用这个例子实验
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.
但是,如果滚动视图 contentOffset
是非零和设备从纵向旋转为横向,内容滚动视图的偏移量自动设置为 32PX
。 (见截图)
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)
我试图挽救 contentOffset
键,将它设置为这个保存的值时, scrollViewDidEndDecelerating:
称为其作品,但为丑陋的滚动视图滚动到一个偏移32PX,然后回到我希望它是。
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.
我如何控制滚动视图的 contentOffset
?是自动版式约束错了吗?是否有额外的限制,我可以添加到控制缩放视图时 contentOffset
?
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?
推荐答案
凡32PX从何而来?这是否与你的左边和右边滚动视图保证金?
Where the 32px comes from? Is it related to your left and right scrollView margin?
是否保持了错误的偏移每次更改网页的时间?如果是的话,你应该看看你的滚动视图的contentInsets值。
Does it keep the wrong offset every time you change page ? If that the case, you should look at your scrollView's contentInsets values.
否则,我做什么,在滚动视图管理与旋转分页观察滚动视图的contentSize:
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:
[self.scrollView addObserver:self forKeyPath:NSStringFromSelector(@selector(contentSize)) options:0 context:nil];
然后,当contentSize值变化,调整contentOffset:
Then, when the contentSize value change, adjust the contentOffset:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.scrollView && [keyPath isEqualToString:NSStringFromSelector(@selector(contentSize))]) {
//Note that you should track your page index
self.scrollView.contentOffset = CGPointMake(self.pageIndex * self.scrollView.bounds.size.width, self.scrollView.contentOffset.y);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
最后,取出观察,当你卸载滚动视图:
Finally, remove the observer when you unload the scrollView:
[self.scrollView removeObserver:self forKeyPath:NSStringFromSelector(@selector(contentSize)) context:nil];
这篇关于自动布局正在发生变化UIScrollView的contentOffset轮换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!