我正在嘗試以編程方式設定帶有乘數的視圖的水平中心約束。但是我得到的始終是1.0乘數的約束。這就是我所做的:
private func createHalfCenteredView() {
let newView = UIView(frame: .zero)
newView.backgroundColor = .systemTeal
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
let top = newView.topAnchor.constraint(equalTo: view.topAnchor)
let bottom = newView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
let width = newView.widthAnchor.constraint(equalToConstant: 100)
let center = newView.centerXAnchor.constraint(equalToSystemSpacingAfter: view.centerXAnchor,
multiplier: 0.5)
NSLayoutConstraint.activate([top, bottom, width, center])
newView.setNeedsUpdateConstraints()
view.setNeedsLayout()
view.layoutIfNeeded()
}
我嘗試使用lessThanOrEqualToSystemSpacingAfter而不是,equalToSystemSpacingAfter但它仍然是一樣的。乘數始終為 1.0 或正好在中間。
有人可以幫我嗎?謝謝。
uj5u.com熱心網友回復:
您不能使用輔助函式使用乘數,試試這種方式
let center = NSLayoutConstraint(item: newView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 0.5, constant: 0)
參考答案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437377.html
下一篇:無法從清單中扣除總工資
