2017-08-07 56 views
1

我有stackview在我的银行代码UIStackView不可见

let comment1 = Comment(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
let comment2 = Comment(frame: CGRect(x: 0, y: 100, width: 100, height: 100)) 
let comment3 = Comment(frame: CGRect(x: 0, y: 200, width: 100, height: 100)) 

let stackView = UIStackView() 
stackView.axis = UILayoutConstraintAxis.vertical 
stackView.distribution = UIStackViewDistribution.equalSpacing 
stackView.alignment = UIStackViewAlignment.center 
stackView.spacing = 16.0 
stackView.backgroundColor = .green 
stackView.translatesAutoresizingMaskIntoConstraints = false 

stackView.addArrangedSubview(comment1) 
stackView.addArrangedSubview(comment2) 
stackView.addArrangedSubview(comment3) 

view.addSubview(stackView) 

stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true 
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true 
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 

Comment是UIView.The问题的一个子类是,stackView也不是子视图是visible.Why是什么?

+0

你好,哪里哟üexcatly添加你的筹码看法? –

+0

@MohammadBashirSidani在最后一个'addArrangedSubview' – sakoaskoaso

+0

后面你调用'self.view.addSubview(stackView)'? –

回答

1

我刚刚用您的代码进行了测试。你的约束似乎存在问题。我添加了heightAnchor和widthAchor的约束,导致在显示stackView之前,它不可见。下面是 是代码和附加的图像。

注:因为我没有评论视图,所以我只使用UIView。您需要将其改回评论

let comment1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
let comment2 = UIView(frame: CGRect(x: 0, y: 100, width: 100, height: 100)) 
let comment3 = UIView(frame: CGRect(x: 0, y: 200, width: 100, height: 100)) 

comment1.heightAnchor.constraint(equalToConstant: 100).isActive = true 
comment1.widthAnchor.constraint(equalToConstant: 100.0).isActive = true 

comment2.heightAnchor.constraint(equalToConstant: 100).isActive = true 
comment2.widthAnchor.constraint(equalToConstant: 100.0).isActive = true 

comment3.heightAnchor.constraint(equalToConstant: 100).isActive = true 
comment3.widthAnchor.constraint(equalToConstant: 100.0).isActive = true 

comment1.backgroundColor = UIColor.blue 
comment2.backgroundColor = UIColor.green 
comment3.backgroundColor = UIColor.red 

let stackView = UIStackView() 
stackView.axis = UILayoutConstraintAxis.vertical 
stackView.distribution = UIStackViewDistribution.equalSpacing 
stackView.alignment = UIStackViewAlignment.center 
stackView.spacing = 16.0 
stackView.backgroundColor = .green 
stackView.translatesAutoresizingMaskIntoConstraints = false 

stackView.addArrangedSubview(comment1) 
stackView.addArrangedSubview(comment2) 
stackView.addArrangedSubview(comment3) 


self.view.addSubview(stackView) 


stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true 
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true 
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 

enter image description here