0

ラベルを含む単純な UIView サブクラスがあります。このサブクラスには、次の init メソッドがあります。

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)
}

実際には、ラベルのテキストは実行時に変更される可能性がありますが、とにかく、アプリを実行すると、ラベルが親ビュー内で垂直方向に中央に配置されません...誰かがこれを手伝ってくれますか?

前もって感謝します

4

1 に答える 1

0

これを参照してください。

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)
于 2016-03-11T13:07:16.220 に答える