gpt4 book ai didi

swift - 如何将约束转换为 Item 或 Item 从 viewA 到 viewB?我收到此错误 : Unknown layout attribute

转载 作者:行者123 更新时间:2023-11-28 13:46:00 25 4
gpt4 key购买 nike

作为标题,我有一个包含 viewA 的 viewParent,但是 viewA 将在 awakeAfter 方法中被 viewB 替换。

我想转换所有约束

from viewParent <-> viewA,

to viewParent <-> viewB.

非常感谢。

这是我的代码:

private func convertConstraints(from: UIView, to: UIView){
from.constraints.forEach { (constraint: NSLayoutConstraint) in
let isFirst = (constraint.firstItem as? UIView) == from
let isSecond = (constraint.secondItem as? UIView) == from

let first: Any = (isFirst) ? to : constraint.firstItem as Any
let second: Any? = (isSecond) ? to : constraint.secondItem as? Any

let new = NSLayoutConstraint(item: first,
attribute: constraint.firstAttribute,
relatedBy: constraint.relation,
toItem: second,
attribute: constraint.secondAttribute,
multiplier: constraint.multiplier,
constant: constraint.constant)
new.priority = constraint.priority
new.identifier = constraint.identifier
new.shouldBeArchived = constraint.shouldBeArchived
new.isActive = constraint.isActive
}
}

控制台日志如下:

2019-03-27 15:36:33.740004+0800 Cale[86571:50392102] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSLayoutConstraint for <Cale.AirportButton: 0x7f9eb454d990; baseClass = UIButton; frame = (0 129; 120 120); autoresize = RM+BM; layer = <CALayer: 0x600003936460>>: Unknown layout attribute'
*** First throw call stack:
(
0 CoreFoundation 0x000000010bf501bb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010b4ee735 objc_exception_throw + 48
2 CoreFoundation 0x000000010bf50015 +[NSException raise:format:] + 197
3 Foundation 0x000000010a16cf61 ResolveConstraintArguments + 372
4 Foundation 0x000000010a16d2cb +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] + 80
.
.
.

最佳答案

我修好了,...非常感谢。

我修改了以下代码:

let second: Any? = (isSecond) ? to : constraint.secondItem as? Any

到:

let second: Any? = (isSecond) ? to : constraint.secondItem

关于swift - 如何将约束转换为 Item 或 Item 从 viewA 到 viewB?我收到此错误 : Unknown layout attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55372094/

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