I am trying to get the reference of topConstraint (eg: attached) of a given view, associated with safe area - top.
I have created a function which works fine, but, logically not reliable.
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
}
I would like to create a function which compares as follows
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
}
How do I compare if the firstItem
or secondItem
is Safe Area.Top
?