layoutBox / FlexLayout

FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax.
MIT License
2k stars 226 forks source link

Why is the subview constraint incorrect? #193

Open shmilyQin opened 2 years ago

shmilyQin commented 2 years ago

Thanks for creating a really nice library, but I'm a little confused right now. Constraints not working after using FlexLayout for subviews

class CellsViewController: UIViewController {

override func viewDidLoad() { super.viewDidLoad() view.flex.define { flex in flex.addItem(infoView).height(60).marginTop(64) } } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() view.flex.layout() } lazy var infoView: AnimationView = { let value = AnimationView(frame: .zero) return value }()

}

class AnimationView: UIView { override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .green flex.paddingHorizontal(12) .alignItems(.center) .direction(.row) .define { flex in flex.addItem(doubleTitileView) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } lazy var doubleTitileView: DoubleTitleView = { let value = DoubleTitleView(frame: .zero) return value }() override func layoutSubviews() { super.layoutSubviews() self.layout() } func layout(){ self.flex.layout() } }

class DoubleTitleView: UIView { override init(frame: CGRect) { super.init(frame: frame) initInterface() }

required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func initInterface() { self.flex.direction(.column) .justifyContent(.center) .define { flex in flex.addItem(titleLabel) flex.addItem(subLabel) } } override func layoutSubviews() { super.layoutSubviews() self.flex.layout() } lazy var titleLabel: UILabel = { let value = UILabel() value.font = UIFont.systemFont(ofSize: 16) value.text = "textOne" return value }() lazy var subLabel: UILabel = { let value = UILabel() value.font = UIFont.systemFont(ofSize: 14) value.text = "textOneTwo" return value }() }

image

The padding and alignItems doesn`t work

zhuolingzhao007 commented 1 year ago

remove the subview 's layoutsubviews() method. I don't know why