-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AloeStackView as subview & Auto-Layout #40
Comments
Hmm first guess:
Tell me if this is enough. |
Here is a quick example to illustrate what I wanted to say: import UIKit
import AloeStackView
class ViewController: UIViewController {
let customView0: CustomView = {
let view = CustomView()
view.translatesAutoresizingMaskIntoConstraints = false
view.label.text = "View0"
view.backgroundColor = UIColor.red
return view
}()
let customView1: CustomView = {
let view = CustomView()
view.translatesAutoresizingMaskIntoConstraints = false
view.label.text = "View1"
view.backgroundColor = UIColor.cyan
return view
}()
let aloeStackView = AloeStackView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(customView0)
view.addSubview(customView1)
view.addSubview(aloeStackView)
NSLayoutConstraint.activate([
customView0.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0),
customView0.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0),
customView0.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0),
customView0.heightAnchor.constraint(equalToConstant: 200)
])
NSLayoutConstraint.activate([
customView1.topAnchor.constraint(equalTo: self.customView0.bottomAnchor, constant: 0),
customView1.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0),
customView1.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0),
customView1.heightAnchor.constraint(equalToConstant: 300)
])
// This line enables the Auto-Layout mode for the AloeStackView
aloeStackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
aloeStackView.topAnchor.constraint(equalTo: self.customView1.bottomAnchor, constant: 0),
aloeStackView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0),
aloeStackView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0),
aloeStackView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0)
])
aloeStackView.rowInset = .zero
setupViews()
}
private func setupViews() {
let view100 = CustomView()
view100.label.text = "100"
view100.backgroundColor = UIColor.red
view100.heightAnchor.constraint(equalToConstant: 100).isActive = true
aloeStackView.addRow(view100)
let view200 = CustomView()
view200.backgroundColor = UIColor.green
view200.heightAnchor.constraint(equalToConstant: 200).isActive = true
view200.label.text = "200"
aloeStackView.addRow(view200)
let view300 = CustomView()
view300.backgroundColor = UIColor.purple
view300.heightAnchor.constraint(equalToConstant: 300).isActive = true
view300.label.text = "300"
aloeStackView.addRow(view300)
}
}
class CustomView: UIView {
let label: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.boldSystemFont(ofSize: 20)
return label
}()
override init(frame: CGRect) {
super.init(frame: .zero)
self.addSubview(label)
label.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
} This should work as expected without any warning in the console. |
Thanks so much for the quick answer! By looking at your example I've found what I was doing wrong: I was setting up the constraints in Everything is fine now, thanks again! :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there! First let me thank you for this great piece of work, I'm using it a lot in my apps and it's great!
I'm having some troubles though with the following settings:
I manage to display things the way I want using autolayout but I have a lot of errors in the console.
As I said, my UI is working but I'm worried about those errors.
How should one setup autolayout for an AloeStackView used as a subview?
Thanks a lot!
Ladislas
The text was updated successfully, but these errors were encountered: