sakoaskoaso
sakoaskoaso

Reputation: 347

UIStackView not visible

I have stackview in my Swift code

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 is a subclass of UIView.The problem is that the stackView nor the subviews are visible.Why is that?

Upvotes: 1

Views: 2061

Answers (1)

sarosh mirza
sarosh mirza

Reputation: 702

I Just tested with your code. There seems to be the problem with your constraints. I added the constraints for heightAnchor and widthAchor which resulted in showing the stackView, before it was not visible. below is the code and also the image attached.

Note: since i didn't had Comment view so i used simply UIView. You will need to change it back to Comment

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

Upvotes: 1

Related Questions