iOS限制不允许使用乘数 [英] iOS constraints doesn't allow to use multiplier
问题描述
我正在尝试布局一些自定义视图,并且当我尝试激活约束时,Xcode说我不能使用乘数.这是代码示例:
I am trying to layout some custom views and when I try to activate the constraints, Xcode says that I can't use multiplier. Here is an example of the code:
class MenuView: UIView {
var addButton: AddButton!
var settingsButton: SettingsButton!
// ........
func setConstraints (withBarReference reference: NSLayoutYAxisAnchor) {
NSLayoutConstraints.activateConstraints([
// ........
addButton.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor, multiplier: 0.5),
// ........
settingsButton.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor, multiplier: 1.5)
])
}
}
这里的问题是Xcode给出了contointEqualToAnchor:函数的语法错误,并说我应该将乘数"替换为常量".
The thing here is that Xcode gives a syntax error on the contraintEqualToAnchor: functions and says that I should replace "multiplier" to "constant".
为什么不能在X中心锚点上使用乘数选项?
Why can't I use the multiplier option with the X center anchors?
推荐答案
您不能使用辅助函数来设置乘数,但是可以使用NSLayoutConstraint初始化程序来设置乘数.自己被这个卡住了,但是找到了答案.
You can't set multiplier using helper functions, but you can set multiplier using NSLayoutConstraint initializer. Just got stuck by this myself, but found the answer.
您的代码:addButton.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor, multiplier: 0.5)
正确的代码:NSLayoutConstraint(item: addButton, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 0.5, constant: 0)
此外,不要忘记通过键入isActive = true
Also, don't forget to activate this constraint by typing isActive = true
这篇关于iOS限制不允许使用乘数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!