0
的最小數量我在我的ViewController下面的代碼對準stackview以編程不使用硬編碼的CGRect和約束
let imageView = UIImageView()
imageView.backgroundColor = UIColor.blueColor()
imageView.heightAnchor.constraintEqualToConstant(120.0).active = true
imageView.widthAnchor.constraintEqualToConstant(120.0).active = true
imageView.image = UIImage(named: "buttonFollowCheckGreen")
let textLabel = UILabel()
textLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true
textLabel.heightAnchor.constraintEqualToConstant(20.0).active = true
textLabel.text = "Hi World"
textLabel.font = UIFont(name:kFontName, size:24)
textLabel.textAlignment = .Center
let stackView = UIStackView()
stackView.axis = UILayoutConstraintAxis.Vertical
// stackView.distribution = UIStackViewDistribution.EqualSpacing
// stackView.alignment = UIStackViewAlignment.Center
stackView.spacing = 16.0
stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(textLabel)
stackView.translatesAutoresizingMaskIntoConstraints = false;
self.view.addSubview(stackView)
stackView.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor).active = true
stackView.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor).active = true
我想有它呈現如下:
上述代碼的最小更改是如何實現的。我希望stackview上的一個屬性可以將它對齊到頂部。
代碼中的最後兩行將您的視圖中的堆棧視圖居中。你可以先改變它。 – Abizern
即使我註釋掉您提到的兩行(即原始問題中也編輯過),我也得到了相同的結果 – Das
您用什麼約束替換它們? – Abizern