1
我想在textview中存在字符时调整和重新定位自动布局约束。是的,我正在使用一个textview委托。是的,视图加载时视图的布局正确。问题出现时,我尝试调整视图......当我改变了width属性的观点激发,距离消失我可以移动我多么希望(的centerX),但是...在更改自动布局约束时遇到问题
下面的代码...
paddingView.widthAnchor.constraint(equalTo:view.widthAnchor,乘数:0.5).isActive =真
NSLayoutConstraint(item: paddingView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 0.75, constant: 0).isActive = true
paddingView.heightAnchor.constraint(equalToConstant: 6.0).isActive = true
bottomConstraint = paddingView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0)
bottomConstraint?.isActive = true
这里的第二部分
func textViewDidChange(_ textView: UITextView) {
let currentString: String = textView.text!
let length: Int = (currentString.characters.count)
if length > 0 {
NSLayoutConstraint(item: paddingView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
paddingView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.75).isActive = true
}else{
}
UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed) in
})
}
难道是因为我一起使用锚和NSLayoutConstraints?
你检查了你的调试输出不可满足的限制吗?请注意,如果多次调用“textViewDidChange:”,它将继续添加新的约束而不删除以前添加的约束,这可能会导致一些自动布局问题 – crizzis
是的,我的一个约束是不可满足的。这是新的宽度约束,它试图通过打破centerX约束来修复它 – Stefan
看看这个:https://stackoverflow.com/questions/19593641/can-i-change-multiplier-property-for-nslayoutconstraint –