我正在使用以下代碼來限制視圖到父UIScrollView的左側和右側錨點。UIScrollView子視圖不擴展爲填充寬度(Autolayout)
儘管右側錨點和左側錨點被設置爲ScrollView的左側和右側錨點,但視圖不會展開以填充滾動視圖。
注意:此圖像中的灰色背景是UIScrollView的背景,所以我知道它適合於其父視圖。
代碼:
self.wtfView.translatesAutoresizingMaskIntoConstraints = false
self.wtfView.backgroundColor = UIColor.orange
self.wtfView.topAnchor.constraint(equalTo: self.passwordField.bottomAnchor, constant: 40.0).isActive = true
self.wtfView.leftAnchor.constraint(equalTo: self.containerView.leftAnchor, constant: 40.0).isActive = true
self.wtfView.rightAnchor.constraint(equalTo: self.containerView.rightAnchor, constant: 40.0).isActive = true
self.wtfView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
self.wtfView.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 40.0).isActive = true
編輯: 下面的代碼工作正常,但我寧願使用左+右錨技術,以指定的寬度,而不是在一個寬度約束。這不應該是可能的嗎?
self.wtfView.translatesAutoresizingMaskIntoConstraints = false
self.wtfView.backgroundColor = UIColor.orange
self.wtfView.topAnchor.constraint(equalTo: self.passwordField.bottomAnchor, constant: 40.0).isActive = true
self.wtfView.leftAnchor.constraint(equalTo: self.containerView.leftAnchor, constant: 40.0).isActive = true
self.wtfView.widthAnchor.constraint(equalTo: self.containerView.widthAnchor, constant: -80.0).isActive = true //THE DIFFERENT ONE
self.wtfView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
self.wtfView.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 040.0).isActive = true
我會給這個鏡頭一點點,但不應該寬度約束與左右錨點碰撞? – orion
所以,這對我有用。如果我將左約束保持在40,並用-80添加一個寬度約束,它看起來是正確的,但不應該提供一個左和右的錨點是否足夠? – orion
多數民衆贊成在我也想過,但經過長時間調試發現,需要提供寬度 – AlekseiPetrovski