refactored layoutConstrainable for methods

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-07-18 12:26:12 -05:00
parent 6661e60333
commit bf40368dc4

View File

@ -705,11 +705,11 @@ extension LayoutConstraintable {
}
// Method to check if the view is pinned to its superview
public func isPinnedToSuperview() -> Bool {
isPinnedVerticallyToSuperview() && isPinnedHorizontallyToSuperview()
public func isPinnedEqual() -> Bool {
isPinnedEqualVertically() && isPinnedEqualHorizontally()
}
public func horizontalPinnedSize() -> CGSize? {
public func horizontalPinnedWidth() -> CGFloat? {
guard let view = self as? UIView, let superview = view.superview else { return nil }
let constraints = superview.constraints
@ -735,36 +735,36 @@ extension LayoutConstraintable {
if let leadingView = leadingObject as? UIView, let trailingView = trailingObject as? UIView {
let leadingPosition = leadingView.convert(leadingView.bounds.origin, to: superview).x
let trailingPosition = trailingView.convert(trailingView.bounds.origin, to: superview).x + trailingView.bounds.width
return CGSize(width: trailingPosition - leadingPosition, height: view.bounds.size.height)
return trailingPosition - leadingPosition
} else if let leadingGuide = leadingObject as? UILayoutGuide, let trailingGuide = trailingObject as? UILayoutGuide {
let leadingPosition = leadingGuide.layoutFrame.minX
let trailingPosition = trailingGuide.layoutFrame.maxX
return CGSize(width: trailingPosition - leadingPosition, height: view.bounds.size.height)
return trailingPosition - leadingPosition
} else if let leadingView = leadingObject as? UIView, let trailingGuide = trailingObject as? UILayoutGuide {
let leadingPosition = leadingView.convert(leadingView.bounds.origin, to: superview).x
let trailingPosition = trailingGuide.layoutFrame.maxX
return CGSize(width: trailingPosition - leadingPosition, height: view.bounds.size.height)
return trailingPosition - leadingPosition
} else if let leadingGuide = leadingObject as? UILayoutGuide, let trailingView = trailingObject as? UIView {
let leadingPosition = leadingGuide.layoutFrame.minX
let trailingPosition = trailingView.convert(trailingView.bounds.origin, to: superview).x + trailingView.bounds.width
return CGSize(width: trailingPosition - leadingPosition, height: view.bounds.size.height)
return trailingPosition - leadingPosition
}
} else if let pinnedObject = leadingPinnedObject {
if let view = pinnedObject as? UIView {
return view.bounds.size
return view.bounds.size.width
} else if let layoutGuide = pinnedObject as? UILayoutGuide {
return layoutGuide.layoutFrame.size
return layoutGuide.layoutFrame.size.width
}
} else if let pinnedObject = trailingPinnedObject {
if let view = pinnedObject as? UIView {
return view.bounds.size
return view.bounds.size.width
} else if let layoutGuide = pinnedObject as? UILayoutGuide {
return layoutGuide.layoutFrame.size
return layoutGuide.layoutFrame.size.width
}
}
@ -772,7 +772,69 @@ extension LayoutConstraintable {
return nil
}
public func isPinnedHorizontallyToSuperview() -> Bool {
public func verticalPinnedHeight() -> CGFloat? {
guard let view = self as? UIView, let superview = view.superview else { return nil }
let constraints = superview.constraints
var topPinnedObject: AnyObject?
var bottomPinnedObject: AnyObject?
for constraint in constraints {
if (constraint.firstItem === view && (constraint.firstAttribute == .top || constraint.firstAttribute == .topMargin)) {
topPinnedObject = constraint.secondItem as AnyObject?
} else if (constraint.secondItem === view && (constraint.secondAttribute == .top || constraint.secondAttribute == .topMargin)) {
topPinnedObject = constraint.firstItem as AnyObject?
} else if (constraint.firstItem === view && (constraint.firstAttribute == .bottom || constraint.firstAttribute == .bottomMargin)) {
bottomPinnedObject = constraint.secondItem as AnyObject?
} else if (constraint.secondItem === view && (constraint.secondAttribute == .bottom || constraint.secondAttribute == .bottomMargin)) {
bottomPinnedObject = constraint.firstItem as AnyObject?
}
}
// Ensure both top and bottom pinned objects are identified
if let topObject = topPinnedObject, let bottomObject = bottomPinnedObject {
// Calculate the size based on the pinned objects
if let topView = topObject as? UIView, let bottomView = bottomObject as? UIView {
let topPosition = topView.convert(topView.bounds.origin, to: superview).y
let bottomPosition = bottomView.convert(bottomView.bounds.origin, to: superview).y + bottomView.bounds.height
return bottomPosition - topPosition
} else if let topGuide = topObject as? UILayoutGuide, let bottomGuide = bottomObject as? UILayoutGuide {
let topPosition = topGuide.layoutFrame.minY
let bottomPosition = bottomGuide.layoutFrame.maxY
return bottomPosition - topPosition
} else if let topView = topObject as? UIView, let bottomGuide = bottomObject as? UILayoutGuide {
let topPosition = topView.convert(topView.bounds.origin, to: superview).y
let bottomPosition = bottomGuide.layoutFrame.maxY
return bottomPosition - topPosition
} else if let topGuide = topObject as? UILayoutGuide, let bottomView = bottomObject as? UIView {
let topPosition = topGuide.layoutFrame.minY
let bottomPosition = bottomView.convert(bottomView.bounds.origin, to: superview).y + bottomView.bounds.height
return bottomPosition - topPosition
}
} else if let pinnedObject = topPinnedObject {
if let view = pinnedObject as? UIView {
return view.bounds.size.height
} else if let layoutGuide = pinnedObject as? UILayoutGuide {
return layoutGuide.layoutFrame.size.height
}
} else if let pinnedObject = bottomPinnedObject {
if let view = pinnedObject as? UIView {
return view.bounds.size.height
} else if let layoutGuide = pinnedObject as? UILayoutGuide {
return layoutGuide.layoutFrame.size.height
}
}
return nil
}
public func isPinnedEqualHorizontally() -> Bool {
guard let view = self as? UIView, let superview = view.superview else { return false }
let constraints = superview.constraints
var leadingPinned = false
@ -796,7 +858,7 @@ extension LayoutConstraintable {
return leadingPinned && trailingPinned
}
public func isPinnedVerticallyToSuperview() -> Bool {
public func isPinnedEqualVertically() -> Bool {
guard let view = self as? UIView, let superview = view.superview else { return false }
let constraints = superview.constraints
var topPinned = false