ScrollView不滚动到视图的尽头 [英] ScrollView Not Scrolling to The End of The View
问题描述
在ViewController中,我使用StoryBoard(在ScrollView和View上设置了约束)在ScrollView中添加了一个ScrollView和一个视图.现在在我的代码中,我正在使用视图来动态添加其他视图.正在添加视图,但我的问题与滚动有关.我可以滚动,但无法滚动到最后一个视图...我尝试执行以下
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
}
但是这没有用,我也注意到内容视图(我正在添加视图的视图)高度在viewDidLayoutSubviews()
中打印了736,即使我添加了更多视图,这当然也应该增加它的大小.
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.
请提供任何帮助.
我正在将视图添加到内容视图中,如下所示:
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
推荐答案
成功了!
像这样编辑viewDidLayoutSubviews:
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
}
这篇关于ScrollView不滚动到视图的尽头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!