Ios 将标签与PureLayout垂直对齐
我有一个简单的UIView子类,它包含一个标签。此子类具有以下init方法:Ios 将标签与PureLayout垂直对齐,ios,uiview,alignment,uilabel,pure-layout,Ios,Uiview,Alignment,Uilabel,Pure Layout,我有一个简单的UIView子类,它包含一个标签。此子类具有以下init方法: override init(frame: CGRect) { super.init(frame: frame) self.autoSetDimension(.Height, toSize: 44) titleLabel.backgroundColor = UIColor.clearColor() titleLabel.translatesAutoresizingMaskIntoCons
override init(frame: CGRect) {
super.init(frame: frame)
self.autoSetDimension(.Height, toSize: 44)
titleLabel.backgroundColor = UIColor.clearColor()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.numberOfLines = 0
self.addSubview(titleLabel)
titleLabel.text = "Some text"
titleLabel.sizeToFit()
titleLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: 20)
titleLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 20)
titleLabel.autoAlignAxisToSuperviewAxis(.Horizontal)
}
实际上,标签的文本在运行时可能会更改,但无论如何,当我运行应用程序时,我看不到标签在其父视图中垂直居中。。。有人能帮我吗
提前谢谢看看这个,如果它能帮到你
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Some dummy text"
self.view.addSubview(label)
let centreH = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 1)
let centreV = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 1)
self.view.addConstraint(centreH)
self.view.addConstraint(centreV)
什么是PureLayout?有自动布局库吗?@BharatModi