问题
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