gpt4 book ai didi

子类中的 Swift 约束

转载 作者:行者123 更新时间:2023-11-30 10:02:24 25 4
gpt4 key购买 nike

enter image description here enter image description here我想知道是否有一种方法可以从子类访问约束。我有一个自定义 View 。我不确定是否可以访问已在 UI 中设置的约束,因此我尝试从子类本身创建约束。

class PerformanceView: UIView {
func initialize() {
let heightConstraint = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: superview, attribute: .Height, multiplier: 0.47, constant: 0);
superview.addConstraint(heightConstraint)
}
}

上面的代码并不完整。只是展示我正在尝试做的事情。那么两个问题。

1) 我可以从子类访问 UI 上的约束吗?2)如果没有,如何在子类中创建一个 super View 高度一半的高度约束。

我确实将 PerformanceView 作为 View 的类,因为您可以看到它们都在左侧说“性能 View ”。其中 4 个。

最佳答案

如果我正确理解你的问题,答案是肯定的,有办法的。您只需使用助理编辑器将约束条件的导出拖到 PerformanceView 中即可。

enter image description here

作为辅助 - 您可以使用 .constant 修改代码中的约束。但目前不支持修改优先级。

heightConstraint.constant = 230.0 //or any other value

为了拖动导出,您必须确保在 Storyboard 编辑器中正确识别 PerformanceView。因此,请在左栏中选择您的 PerformanceView。然后在右列突出显示的字段中输入“PerformanceView”(如下所示),然后按回车键。现在您应该可以开始了。

enter image description here

关于子类中的 Swift 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37735590/

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