How to set content size of UIScrollView dynamically

前端 未结 11 1919
耶瑟儿~
耶瑟儿~ 2020-12-01 05:41

I got question about UIScrollview.

The story is I have a UIView named ChartsView which I re-draw it myself by override method drawRec

11条回答
  •  星月不相逢
    2020-12-01 05:44

    swift (2.0)

    @IBOutlet weak var btnLatestButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let height = btnLatestButton.frame.size.height
        let pos = btnLatestButton.frame.origin.y
        let sizeOfContent = height + pos + 10
        scrollview.contentSize.height = sizeOfContent
    
    }
    

    Above ofcourse is when you just have a fixed amount of views on your scrollview. IOS Rocks method is available but was not good for me, created more space then I needed.

        let lastView : UIView! = scrollview.subviews.last
        let height = lastView.frame.size.height
        let pos = lastView.frame.origin.y
        let sizeOfContent = height + pos + 10
        scrollview.contentSize.height = sizeOfContent
    

提交回复
热议问题