UIScrollview和自动布局

也许有时候我们想使用UIScrollView来显示列表内容,而不是使用UIPagesControllerUICollectionView 。 并且在这种情况下使用“自动布局”可以使声明布局更加明确。

看看Github上的示例项目https://github.com/onmyway133/archives/tree/master/ScrollViewAutolayout

这个想法是使用UIView作为UIScrollViewcontentView来确定其contentSize

首先,固定UIScrollView的边缘

其次,固定contentView的边缘

对于contentView内部的视图,将约束水平固定,以便将最左侧的视图固定到contentView的左侧,将最右侧的视图固定到contentView的右侧。 对于高度,我们将contentView的顶部和右侧边缘固定到其父级,即UIScrollView

参考文献

  • https://developer.apple.com/library/content/technotes/tn2154/_index.html
  • http://samwize.com/2014/03/14/how-to-use-uiscrollview-with-autolayout/
  • https://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/