0
我想在所有的編程約束條件下都變得更好。Swift Programatic約束view.bottomAndchor問題
我創建了一個界面表示形式,我想要確保所有內容都變成了藍色,並且運行時沒有發生錯誤。
在界面生成器中,我創建了一個UIView,將比率設置爲1:1,頂部錨點100和底部錨點450,並水平約束它。
的偉大工程。
現在我刪除了這一切,並通過代碼做
我想,這應該是相同的IB版本...
let testView = UIView()
view.addSubview(testView)
testView.backgroundColor = .red
let testViewTopConstraint = testView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100)
let testViewBottomConstraint = testView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 450)
let testViewCenterXConstraint = testView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let testViewAspectConstraint = NSLayoutConstraint(item: testView,
attribute: .height,
relatedBy: .equal,
toItem: testView,
attribute: .width,
multiplier: (1.0/1.0),
constant: 0)
NSLayoutConstraint.activate([testViewTopConstraint, testViewBottomConstraint, testViewCenterXConstraint, testViewAspectConstraint])
testView.translatesAutoresizingMaskIntoConstraints = false
當我運行它,紅色正方形變成長方形延伸到最後(可能超越)。
我玩過程序化的約束。如果我要添加一個主要的約束條件,我就完全沒問題,它似乎是我的底層約束條件,總是讓我困惑起來。
我的底部約束有什麼問題? 還是我在做別的事情?