refactor for layoutguide
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
25ebcff2d9
commit
8dfcb41691
@ -709,6 +709,69 @@ extension LayoutConstraintable {
|
||||
isPinnedVerticallyToSuperview() && isPinnedHorizontallyToSuperview()
|
||||
}
|
||||
|
||||
public func horizontalPinnedSize() -> CGSize? {
|
||||
guard let view = self as? UIView, let superview = view.superview else { return nil }
|
||||
let constraints = superview.constraints
|
||||
|
||||
var leadingPinnedObject: AnyObject?
|
||||
var trailingPinnedObject: AnyObject?
|
||||
|
||||
for constraint in constraints {
|
||||
if (constraint.firstItem === view && (constraint.firstAttribute == .leading || constraint.firstAttribute == .left)) {
|
||||
leadingPinnedObject = constraint.secondItem as AnyObject?
|
||||
} else if (constraint.secondItem === view && (constraint.secondAttribute == .leading || constraint.secondAttribute == .left)) {
|
||||
leadingPinnedObject = constraint.firstItem as AnyObject?
|
||||
} else if (constraint.firstItem === view && (constraint.firstAttribute == .trailing || constraint.firstAttribute == .right)) {
|
||||
trailingPinnedObject = constraint.secondItem as AnyObject?
|
||||
} else if (constraint.secondItem === view && (constraint.secondAttribute == .trailing || constraint.secondAttribute == .right)) {
|
||||
trailingPinnedObject = constraint.firstItem as AnyObject?
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure both leading and trailing pinned objects are identified
|
||||
if let leadingObject = leadingPinnedObject, let trailingObject = trailingPinnedObject {
|
||||
|
||||
// Calculate the size based on the pinned objects
|
||||
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)
|
||||
|
||||
} 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)
|
||||
|
||||
} 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)
|
||||
|
||||
} 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)
|
||||
}
|
||||
|
||||
} else if let pinnedObject = leadingPinnedObject {
|
||||
if let view = pinnedObject as? UIView {
|
||||
return view.bounds.size
|
||||
} else if let layoutGuide = pinnedObject as? UILayoutGuide {
|
||||
return layoutGuide.layoutFrame.size
|
||||
}
|
||||
|
||||
} else if let pinnedObject = trailingPinnedObject {
|
||||
if let view = pinnedObject as? UIView {
|
||||
return view.bounds.size
|
||||
} else if let layoutGuide = pinnedObject as? UILayoutGuide {
|
||||
return layoutGuide.layoutFrame.size
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
public func isPinnedHorizontallyToSuperview() -> Bool {
|
||||
guard let view = self as? UIView, let superview = view.superview else { return false }
|
||||
let constraints = superview.constraints
|
||||
@ -716,16 +779,16 @@ extension LayoutConstraintable {
|
||||
var trailingPinned = false
|
||||
|
||||
for constraint in constraints {
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .leading && constraint.relation == .equal && constraint.secondItem as? UIView == superview) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .leading && constraint.relation == .equal && constraint.firstItem as? UIView == superview) ||
|
||||
(constraint.firstItem as? UIView == view && constraint.firstAttribute == .left && constraint.relation == .equal && constraint.secondItem as? UIView == superview) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .left && constraint.relation == .equal && constraint.firstItem as? UIView == superview) {
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .leading && constraint.relation == .equal) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .leading && constraint.relation == .equal) ||
|
||||
(constraint.firstItem as? UIView == view && constraint.firstAttribute == .left && constraint.relation == .equal) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .left && constraint.relation == .equal) {
|
||||
leadingPinned = true
|
||||
}
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .trailing && constraint.relation == .equal && constraint.secondItem as? UIView == superview) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .trailing && constraint.relation == .equal && constraint.firstItem as? UIView == superview) ||
|
||||
(constraint.firstItem as? UIView == view && constraint.firstAttribute == .right && constraint.relation == .equal && constraint.secondItem as? UIView == superview) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .right && constraint.relation == .equal && constraint.firstItem as? UIView == superview) {
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .trailing && constraint.relation == .equal) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .trailing && constraint.relation == .equal) ||
|
||||
(constraint.firstItem as? UIView == view && constraint.firstAttribute == .right && constraint.relation == .equal) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .right && constraint.relation == .equal) {
|
||||
trailingPinned = true
|
||||
}
|
||||
}
|
||||
@ -740,18 +803,20 @@ extension LayoutConstraintable {
|
||||
var bottomPinned = false
|
||||
|
||||
for constraint in constraints {
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .top && constraint.relation == .equal && constraint.secondItem as? UIView == superview) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .top && constraint.relation == .equal && constraint.firstItem as? UIView == superview) {
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .top && constraint.relation == .equal) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .top && constraint.relation == .equal) {
|
||||
topPinned = true
|
||||
}
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .bottom && constraint.relation == .equal && constraint.secondItem as? UIView == superview) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .bottom && constraint.relation == .equal && constraint.firstItem as? UIView == superview) {
|
||||
if (constraint.firstItem as? UIView == view && constraint.firstAttribute == .bottom && constraint.relation == .equal) ||
|
||||
(constraint.secondItem as? UIView == view && constraint.secondAttribute == .bottom && constraint.relation == .equal) {
|
||||
bottomPinned = true
|
||||
}
|
||||
}
|
||||
|
||||
return topPinned && bottomPinned
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user