gpt4 book ai didi

ios - 获取顶部约束的引用

转载 作者:行者123 更新时间:2023-11-29 05:47:06 25 4
gpt4 key购买 nike

我正在尝试获取与安全区域 - 顶部关联的给定 View 的 topConstraint(例如:附加)的引用。

enter image description here

我创建了一个工作正常的函数,但逻辑上不可靠。

func getSafeAreaTopConstraint(view: UIView, in targetView: UIView) -> NSLayoutConstraint? {
let constraints = targetView.constraints.filter({ $0.firstItem as? UIView == view || $0.secondItem as? UIView == view })
return constraints.filter({ $0.firstAttribute == .top || $0.secondAttribute == .top }).first
}

我想创建一个比较如下的函数

func getSafeAreaTopConstraint(view: UIView, in targetView: UIView) -> NSLayoutConstraint? {
let constraints = targetView.constraints.filter({ ($0.firstItem as? UIView == view && $0.secondItem == SafeArea.Top) || ($0.secondItem as? UIView == view && $0.firstItem == SafeArea.Top) })
return constraints.filter({ $0.firstAttribute == .top || $0.secondAttribute == .top }).first
}

如何比较 firstItemsecondItem 是否为 Safe Area.Top

最佳答案

您通过访问constraints字段所做的事情并不安全。推荐的方法是为约束创建 IBOutlet

关于ios - 获取顶部约束的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56036072/

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