UIScrollView Content Size with Autolayout

守給你的承諾、 提交于 2019-12-12 01:51:20

问题


I am using UIScrollView with Auto-layout to change contentSenter code hereize value as following :

  • UIView
  • ScrollView
  • UIView (contentView)

In run time I am adding UITextView & UIImageView to conentView with constraint(Top,Bottom,Left,Right)

but the contentView size is not changing and UIScrollView contentSize also.

So what could be the problem ?


回答1:


In run time I am adding UITextView & UIImageView to conentView with constraint(Top,Bottom,Left,Right)

Maybe you should add constraint (Top, Left, Height, Width),or (Top, Left, Right, Height)

If you persist in (Top,Bottom,Left,Right), ScrollView will keep the old content size, and make your UITextView & UIImageView (Height = 0, Width = 0).




回答2:


Here's a neat way of updating scroll view's content size. It's Swift so you'll need a bridging header if you're working in 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
    }
}

Just call it on your scroll view's object whenever you add a child view to your scroll view.



来源:https://stackoverflow.com/questions/28023951/uiscrollview-content-size-with-autolayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!