gpt4 book ai didi

ios - 约束或其 anchor 是否引用不同 View 层次结构中的项目?那是违法的

转载 作者:搜寻专家 更新时间:2023-11-01 07:06:36 25 4
gpt4 key购买 nike

我正在尝试以编程方式创建带有约束的 UIButton。它应该根据数组中的项目创建按钮。

我已将 UIView socket 作为 viewSingle 并在 Storyboard 中给出了前导、底部、尾随和高度约束。现在,当我试图以编程方式约束 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] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

谁能帮我解决这个问题?

最佳答案

您的主要问题是每次循环都在创建NEW 按钮:

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 的 subview 。

通过循环的下一次,您创建一个 btnOne -- 然后添加btnSecondviewSingle 并尝试将其限制为 new btnOne,它还没有已添加到 viewSingle

如果您在循环外部创建了 3 个按钮变量 - 例如在您创建 viewSingle 的相同级别,则删除任何 = UIButton(... ) 循环内的语句,你不会得到那个错误。

不确定您的整体 totalItemsmultiplier 计算以及您的其余约束是否真的会按照您的要求进行。您可能需要重新考虑您的方法(您还没有展示/解释您要做什么,所以很难说)。

关于ios - 约束或其 anchor 是否引用不同 View 层次结构中的项目?那是违法的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47676010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com