【问题标题】:Prevent UIScrollView scroll horizontally if auto layout is on如果自动布局打开,防止 UIScrollView 水平滚动
【发布时间】:2013-11-30 11:39:23
【问题描述】:
我有这个带有自动布局的滚动视图。滚动视图具有蓝色。在横向模式下看起来不错。
但在 portrait 模式下,我不会让水平滚动,而是用橙色减小视图的宽度,以保持与右边缘的 20px 距离。
怎么样?我也在右侧设置了 20px 约束,但是旋转时 contentSize 不会更新。我知道在自动布局的情况下,我不允许以编程方式设置 contentSize。
在这里你可以看到我设置的约束:
【问题讨论】:
标签:
ios
uiscrollview
autolayout
【解决方案1】:
我想通了。我没有设置正确的约束,而是为视图宽度创建了约束,为约束创建了一个 IBOutlet,并在代码中设置了约束的常量。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
//self.scrollView.contentSize = CGSizeMake(944, 1600);
_viewWidthConstrain.constant = 944;
} else {
//self.scrollView.contentSize = CGSizeMake(500, 1600);
_viewWidthConstrain.constant = 500;
}
}
结果如下: