水平居中滚动视图的内容

时间:2015-09-09 08:45:06

标签: ios uiscrollview autolayout

我在滚动视图中嵌入了标准视图,我正在使用自动布局。一切都很好,我差不多完成了。只有一个小问题。我的内容视图固定高度为536点,因此在iPhone 6中完全显示而无需滚动,而在iPhone 4S中我必须滚动才能查看其他内容。

我的问题是视图不是水平居中的是iPhone 6所以它在底部显示了额外的空间(为了清晰起见,我用灰色填充它),如下图所示(这是底层的滚动视图)。

scroll view

我的问题是:如何在不弄乱约束的情况下水平居中内容视图?目前,滚动视图固定在视图控制器的四个边框上,内容视图固定在滚动视图的四个边框上。

我很高兴提供您帮助我解决问题所需的任何其他信息。

编辑:这是它对我有用的

CGFloat superViewHeight = self.view.bounds.size.height;
CGFloat scrollViewHeight = self.scrollView.bounds.size.height;

if (superViewHeight > scrollViewHeight)
    self.topConstraint.constant += (superViewHeight - scrollViewHeight) / 4;

1 个答案:

答案 0 :(得分:1)

如果您的内容视图不需要滚动,则应手动调整顶部约束。

CGFloat scrollViewHeight = CGRectGetHeight(scrollView.bounds);
CGFloat contentViewHeight = CGRectGetHeight(contentView.bounds);

if (scrollViewHeight >= contentViewHeight)
{
    topConstraint.constant += ((scrollViewHeight - contentViewHeight) / 2.f);
}