我有一个UIScrollView,我已经添加了一个子视图的UIStackView。子视图不可滚动,但如果我添加按钮而不是UIViews,则堆栈视图是可滚动的。此代码显示在拆分视图控制器的“详细信息视图”中。任何关于为什么子视图不滚动的想法? 顺便说一句,detailItem将携带索引,但是现在我正在使用索引[4]来编码保证长卷轴列表。
import UIKit
class DetailViewController: UIViewController {
var scrollView: UIScrollView!
var stackView: UIStackView!
var detailItem: AnyObject? {
didSet {
// Update the view.
// self.configureView()
// print("didSet: configureView")
}
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Vertical
scrollView.addSubview(stackView)
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[stackView]|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[stackView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
/*
for _ in 1 ..< 100 {
let vw = UIButton(type: UIButtonType.System)
vw.setTitle("Button", forState: .Normal)
stackView.addArrangedSubview(vw)
}
*/
let selectedGroup: Group = GroupArray[4]
let descriptorsArray = selectedGroup.descriptorsArray
for descriptor in descriptorsArray {
// Create a subview for each descriptor
let subView = UIView()
subView.frame = CGRectMake(0 , 0, self.view.frame.width-10, 50)
subView.backgroundColor = UIColor.yellowColor()
subView.heightAnchor.constraintEqualToConstant(50.0).active = true
// Create a label for Descriptor subview
let label = UILabel(frame: CGRectMake(20, 0, 200, 46))
label.text = descriptor.name
label.font = UIFont.boldSystemFontOfSize(22.0)
label.textAlignment = .Left
label.textColor = UIColor.brownColor()
subView.addSubview(label)
// Create a button for Checkbox
stackView.addArrangedSubview(subView)
}
}
}
答案 0 :(得分:1)
您缺少约束:
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[stackView]|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[stackView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
您忘记将堆栈视图固定在滚动视图的底部。没有它,滚动视图没有contentSize
。没有contentSize
,没有滚动。
答案 1 :(得分:0)
@Jolly您可以尝试 ScrollableStackView :https://github.com/gurhub/ScrollableStackView
它的Objective-C和Swift兼容库。它可以通过CocoaPods获得。
示例代码
import ScrollableStackView
var scrollable = ScrollableStackView(frame: view.frame)
view.addSubview(scrollable)
// add your views with
let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 55))
rectangle.backgroundColor = UIColor.blue
scrollable.stackView.addArrangedSubview(rectangle)
// ...