带有Autolay的UIScrollView内容大小

我正在使用UIScrollView自动布局更改内容enter code here ize值如下:

  • UIView
  • ScrollView
  • UIViewcontentView

在运行时,我将UITextViewUIImageView添加到带有约束( conentView ,bottom, conentView ,right)的conentView

contentView大小并没有改变UIScrollView contentSize也是如此。

那么可能是什么问题呢?

在运行时,我将UITextView&UIImageView添加到带有约束(cone,bottom,left,right)的conentView中,

也许你应该添加约束(上,左,高,宽)或(上,左,右,高)

如果你坚持(上,下,左,右),ScrollView将保持旧的内容大小,并使您的UITextView&UIImageView(高= 0,宽= 0)。

这是更新滚动视图的内容大小的一种简洁的方式。 这是Swift,所以如果你在Objective C中工作,你需要一个桥接头。

 extension UIScrollView { func updateContentViewSize() { var newHeight: CGFloat = 0 for view in subviews { let ref = view.frame.origin.y + view.frame.height if ref > newHeight { newHeight = ref } } let oldSize = contentSize let newSize = CGSize(width: oldSize.width, height: newHeight + 20) contentSize = newSize } } 

只要在你的滚动视图中添加一个子视图,就可以在你的滚动视图的对象上调用它。