简体   繁体   English

如何动态调整 UIScrollView 的大小?

[英]How to dynamically size UIScrollView?

I'm adding content to the UIScrollView dynamically.我正在向 UIScrollView 动态添加内容。 The content size of the scroll view is increasing.滚动视图的内容大小正在增加。 How can I change the content size to fit the dynamically added content?如何更改内容大小以适应动态添加的内容? See the code I'm using that is not working查看我正在使用的代码不起作用

extension UIScrollView {
    func updateContentView() {
        contentSize.height = subviews.sorted(by: { $0.frame.maxY < $1.frame.maxY }).last?.frame.maxY ?? contentSize.height
        contentSize.height += 300
    }
}

refer this question参考这个问题

I hope the above solves your problem but I'd prefer using a UITableView or a UICollectionView in place of this because everything is handled so smoothly in them.我希望以上内容可以解决您的问题,但我更愿意使用UITableViewUICollectionView来代替它,因为在它们中一切都处理得如此顺利。 You just have to give the number of rows(return a variable which changes dynamically).你只需要给出行数(返回一个动态变化的变量)。 That's it, your contentSize is adjusted internally.就是这样,您的contentSize在内部进行了调整。

But again if you have a different requirement please go with the reference link!但如果您有不同的要求,请使用参考链接致电 go!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM