Swift:使用子视图后滚动视图不起作用 [英] Swift: scrollview after use subviews not work
本文介绍了Swift:使用子视图后滚动视图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式将子视图添加到 scrollview
I want add sub view to the scrollview
programmatically
如果我将 subviews
添加到 scrollview
,滚动不起作用
If i add subviews
to scrollview
, scrolling not work
for subview in view.scrollView.subviews {
subview.removeFromSuperview()
}
var j = 0
for value in getList {
let viewChild = SelectClass.createMyClassView()
viewChild.translatesAutoresizingMaskIntoConstraints = false
viewAll[j] = viewChild
viewChild.title.text = value.value
view.scrollView.addSubview(viewChild)
j = j+1
}
var i = 0
for subview in view.scrollView.subviews {
let item = subview as? UIView
NSLayoutConstraint.activate([
(item?.leftAnchor.constraint(equalTo: view.scrollView.leftAnchor))!,
(item?.rightAnchor.constraint(equalTo: view.scrollView.rightAnchor))!,
(item?.heightAnchor.constraint(equalToConstant: 50))!
])
if(i > 0){
NSLayoutConstraint.activate([
(item?.topAnchor.constraint(equalTo: (viewAll[i-1]?.bottomAnchor)!))!
])
} else {
NSLayoutConstraint.activate([
(item?.topAnchor.constraint(equalTo: view.scrollView.topAnchor))!
])
}
i = i+1
}
view.scrollView.isScrollEnabled = true
我在 scrollView
中尝试了一个视图,但没有再次工作
I try one view in scrollView
but not work again
在普通故事板中工作正常,但通过 addSubView
不起作用
In normal storyboard work fine, but by addSubView
not work
我该如何解决?
推荐答案
首先最好使用 UIStackView
,这里需要为最后一项添加底部约束
First it's better using UIStackView
, here you need to add bottom constraint for the last item
if let last = viewAll.last {
NSLayoutConstraint.activate([
last.bottomAnchor.constraint(equalTo: view.scrollView.bottomAnchor)
])
}
这篇关于Swift:使用子视图后滚动视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文