Calculate the contentsize of scrollview

前端 未结 7 1471
旧巷少年郎
旧巷少年郎 2020-12-13 11:40

I\'m having a scrollview as the detailedview of tableview cell. There are multiple views on the detailedview like labels, buttons etc. which I\'m creating through interface

7条回答
  •  悲哀的现实
    2020-12-13 12:13

    I had the same situation, but then I wrote a new version in Swift 4 mirroring the better answer in Objective-C by Hannes Larsson:

    import UIKit
    
    extension UIScrollView {
        func fitSizeOfContent() {
            let sumHeight = self.subviews.map({$0.frame.size.height}).reduce(0, {x, y in x + y})
            self.contentSize = CGSize(width: self.frame.width, height: sumHeight)
        }
    }
    

提交回复
热议问题