用的UIScrollView的内容自动布局尺寸 [英] UIScrollView Content Size with Autolayout

查看:611
本文介绍了用的UIScrollView的内容自动布局尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的UIScrollView 与自动布局更改的内容输入code此处 IZE值如下

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


  • 的UIView

  • 滚动型

  • 的UIView 内容查看

  • UIView
  • ScrollView
  • UIView (contentView)

在运行时我加入的UITextView &安培; 的UIImageView conentView 与约束(上,下,左,右)

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

内容查看大小的不改变的UIScrollView contentSize 也。

那么,什么可能是问题?

So what could be the problem ?

推荐答案

下面是更新滚动视图内容大小的一种巧妙的方法。这是斯威夫特所以你如果你的目标C的工作需要一个桥接报头。

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.

这篇关于用的UIScrollView的内容自动布局尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆