3
いくつかの制約があるサブビューを追加したいと考えています。制約付きのサブビューを追加できない
しかし、私の意見は表示されません。 以下はいくつかのコードですが、誰かが間違っていることを知っていますか? (私はUITextFieldのを追加した場合、例えば、それが正常に動作します...)
class TestViewController:UIViewController {
override func viewDidLoad() {
//Do not work...
addAndLayout(v: UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0)))
//Works fine
//addAndLayout(v: UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 0)))
}
func addAndLayout(v:UIView) {
v.backgroundColor = UIColor.red
view.addSubview(v)
v.translatesAutoresizingMaskIntoConstraints = false
let leading = NSLayoutConstraint(item: v, attribute: .leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
let trailing = NSLayoutConstraint(item: v, attribute: .trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
let top = NSLayoutConstraint(item: v, attribute: .top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
view.addConstraints([leading, trailing, top])
}
}
問題はありませんが、自動レイアウトはあなたが快適になる前に最も簡単なものではありません。 –