2
,例如這樣。在使用錨定屬性設置約束之後,從UIView獲取nslayoutconstraints
pageControl.anchorWithFixedHeight(nil, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, topConstant: 0, leadingConstant: 0, bottomConstant: 0, trailingConstant: 0, heightConstant: 30)
其中anchorWithFixedHeight就像下面的一個輔助功能:
func anchorWithFixedHeight(_ top: NSLayoutYAxisAnchor? = nil, leading: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, trailing: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leadingConstant: CGFloat = 0, bottomConstant: CGFloat = 0, trailingConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant))
}
if let leading = leading {
anchors.append(leadingAnchor.constraint(equalTo: leading, constant: leadingConstant))
}
if let bottom = bottom {
anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: bottomConstant))
}
if let trailing = trailing {
anchors.append(trailingAnchor.constraint(equalTo: trailing, constant: trailingConstant))
}
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant))
}
現在,比方說,我希望得到底部錨約束來改變其性質,我怎麼能做到這一點?
預先感謝
編輯:這裏的錨是NSlayoutContraint的陣列。我當前的方法以這樣的方式工作,即我將所有需要的約束添加到此數組中,然後獲取任何約束,例如此數組中的底部錨定約束。
但是,我覺得解決方案不夠雄辯。因此,想知道是否還有其他更好的方法。
感謝您的回答。(: – user3608914