ScrollView Not Scrolling to The End of The View

人走茶凉 提交于 2020-01-02 13:51:57

问题


In my ViewController i added a ScrollView and a view inside the ScrollView using StoryBoard (set constraints on ScrollView and the View). now in my code i am using the View to add other views dynamically. the views are being added but my question is related to scrolling. i am able to scroll but it is not scrolling to the last view... i tried doing the following

override func viewDidLayoutSubviews() {
    ScrollView.contentSize.height = contentView.frame.height
}

but this did not work, also i noticed that the content view (view i am adding views to) height is printing 736 in viewDidLayoutSubviews() even when i add more views which of course should increase its size.

please any help would be appreciated.

i am adding the views to content view like this:

let Description = UILabel(frame: CGRectMake(20, CGFloat(position), self.view.frame.size.width-40, 40))
Description.addConstraint(NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
Description.addConstraint(NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: self.view.frame.size.width-40))
contentView.addSubview(Description)
// i don't know if i should add constraints but i added anyway

回答1:


it worked!

edit the viewDidLayoutSubviews like this:

 override func viewDidLayoutSubviews() {

        self.view.setupContentViewForViewWithScroll(contentView: contentView)

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

        ScrollView.contentSize.height = sizeOfContent

    }


来源:https://stackoverflow.com/questions/33666864/scrollview-not-scrolling-to-the-end-of-the-view

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