我正在尝试使用约束以编程方式创建UIButtons。它应该根据数组中的项创建按钮。
我将UIView出口作为viewSingle并在故事板中给出了前导,下限,尾随和高度限制。现在,当我尝试以编程方式约束UIButton时,它会给出下面提到的错误。
for (index,item) in (self.planModel.plan_settings?.sticky_buttons?.enumerated())!{
var btnOne = UIButton()
var btnSecond = UIButton()
var btnThird = UIButton()
let totalItems = self.planModel.plan_settings?.sticky_buttons?.count ?? 1
let multiplier = (UIScreen.main.bounds.width / CGFloat(totalItems)) / UIScreen.main.bounds.width
switch index {
case 0:
btnOne = UIButton(type: UIButtonType.system)
btnOne.backgroundColor = UIColor.green
viewSingle.addSubview(btnOne)
btnOne.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
btnOne.leadingAnchor.constraint(equalTo: viewSingle.leadingAnchor, constant: 0),
btnOne.topAnchor.constraint(equalTo: viewSingle.topAnchor, constant: 0),
// button.widthAnchor.constraint(equalTo: viewSingle!.widthAnchor, constant: 0),
btnOne.heightAnchor.constraint(equalTo: viewSingle!.heightAnchor, constant: 0),
btnOne.widthAnchor.constraint(equalTo: viewSingle.widthAnchor, multiplier: multiplier, constant: 0)
])
self.view.layoutIfNeeded()
case 1:
btnSecond = UIButton(type: UIButtonType.system)
btnSecond.backgroundColor = UIColor.yellow
viewSingle.addSubview(btnSecond)
btnSecond.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
btnSecond.leadingAnchor.constraint(equalTo: btnOne.leadingAnchor, constant: 0),
btnSecond.topAnchor.constraint(equalTo: viewSingle.topAnchor, constant: 0),
// button.widthAnchor.constraint(equalTo: viewSingle!.widthAnchor, constant: 0),
btnSecond.heightAnchor.constraint(equalTo: viewSingle!.heightAnchor, constant: 0),
btnSecond.widthAnchor.constraint(equalTo: viewSingle.widthAnchor, multiplier: multiplier, constant: 0)
])
self.view.layoutIfNeeded()
case 2:
btnThird = UIButton(type: UIButtonType.system)
btnThird.backgroundColor = UIColor.orange
viewSingle.addSubview(btnThird)
btnThird.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
btnThird.leadingAnchor.constraint(equalTo: viewSingle.leadingAnchor, constant: 0),
btnThird.topAnchor.constraint(equalTo: viewSingle.topAnchor, constant: 0),
// button.widthAnchor.constraint(equalTo: viewSingle!.widthAnchor, constant: 0),
btnThird.heightAnchor.constraint(equalTo: viewSingle!.heightAnchor, constant: 0),
btnThird.widthAnchor.constraint(equalTo: viewSingle.widthAnchor, multiplier: multiplier, constant: 0)
])
self.view.layoutIfNeeded()
default:
break
}
}
错误: -
2017-12-06 19:16:03.601661 + 0530 WayOL [8637:3028327] ***由于未捕获的异常'NSGenericException'终止应用程序,原因:'无法使用锚点激活约束,因为它们没有共同的祖先。约束或其锚点是否引用不同视图层次结构中的项目?这是非法的。'
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您的主要问题是,您每次循环都会创建 新 按钮:
for (index,item) in (self.planModel.plan_settings?.sticky_buttons?.enumerated())!{
var btnOne = UIButton()
var btnSecond = UIButton()
var btnThird = UIButton()
// rest of code inside loop
}
因此,第一次循环,index
等于0,并添加btnOne
作为viewSingle
的子视图。
下一次循环时间,您创建 新 btnOne
- 然后添加btnSecond
到viewSingle
并尝试将其约束到 新 btnOne
,但尚未添加到viewSingle
。< / p>
如果你在外面创建了3个按钮vars - 例如你创建viewSingle
的同一级别,那么从循环内部删除任何= UIButton(...)
语句,你不会得到那个错误。
不确定您的整体totalItems
和multiplier
计算以及其他约束是否会实际执行您想要的操作。你可能需要重新思考你的方法(你还没有表明/解释你的目标,很难说)。