From 8429fba539924094cf4cd8a6f9c6e053b6076a8f Mon Sep 17 00:00:00 2001 From: Raul Riera Date: Fri, 25 May 2018 14:26:07 -0400 Subject: [PATCH 1/2] Support for providing a layout to all four edges --- FunctionalTableData/TableItemLayout.swift | 87 +++++++++++++++++++-- FunctionalTableData/UIView+AutoLayout.swift | 84 -------------------- 2 files changed, 80 insertions(+), 91 deletions(-) diff --git a/FunctionalTableData/TableItemLayout.swift b/FunctionalTableData/TableItemLayout.swift index 0c551cf..ba647d1 100644 --- a/FunctionalTableData/TableItemLayout.swift +++ b/FunctionalTableData/TableItemLayout.swift @@ -14,44 +14,117 @@ public protocol TableItemLayout { // TableItemLayout -public typealias EdgeBasedTableItemLayout = CombinedLayout -public typealias LayoutMarginsTableItemLayout = CombinedLayout +public typealias EdgeBasedTableItemLayout = Layout +public typealias LayoutMarginsTableItemLayout = Layout public struct ExplicitLayoutMarginsTableItemLayout: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { contentView.preservesSuperviewLayoutMargins = false - view.constrainToFillView(contentView, respectingLayoutMargins: true) + + MarginsLayout.Top.layoutView(view, inContentView: contentView) + MarginsLayout.Leading.layoutView(view, inContentView: contentView) + MarginsLayout.Bottom.layoutView(view, inContentView: contentView) + MarginsLayout.Trailing.layoutView(view, inContentView: contentView) } } public enum EdgeLayout { + @available(*, deprecated, message: "Use `Leading` and `EdgeLayout.Trailing` instead.") public struct Horizontal: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { - view.constrainToFillViewHorizontally(contentView) + Leading.layoutView(view, inContentView: contentView) + Trailing.layoutView(view, inContentView: contentView) } } + + @available(*, deprecated, message: "Use `EdgeLayout.Top` and `EdgeLayout.Bottom` instead.") public struct Vertical: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { - view.constrainToFillViewVertically(contentView) + Top.layoutView(view, inContentView: contentView) + Bottom.layoutView(view, inContentView: contentView) + } + } + + public struct Top: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true + } + } + + public struct Leading: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true + } + } + + public struct Bottom: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true + } + } + + public struct Trailing: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true } } } + public enum MarginsLayout { + @available(*, deprecated, message: "Use `MarginsLayout.Leading` and `MarginsLayout.Trailing` instead.") public struct Horizontal: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { - view.constrainToFillViewHorizontally(contentView, respectingLayoutMargins: true) + Leading.layoutView(view, inContentView: contentView) + Trailing.layoutView(view, inContentView: contentView) } } + + @available(*, deprecated, message: "Use `MarginsLayout.Top` and `MarginsLayout.Bottom` instead.") public struct Vertical: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { - view.constrainToFillViewVertically(contentView, respectingLayoutMargins: true) + Top.layoutView(view, inContentView: contentView) + Bottom.layoutView(view, inContentView: contentView) + } + } + + public struct Top: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + view.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor).isActive = true + } + } + + public struct Leading: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + view.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor).isActive = true + } + } + + public struct Bottom: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + contentView.layoutMarginsGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true + } + } + + public struct Trailing: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + contentView.layoutMarginsGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true } } } +@available(*, deprecated, message: "Use `Layout(Top:Leading:Bottom:Trailing)` instead.") public struct CombinedLayout: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { Horizontal.layoutView(view, inContentView: contentView) Vertical.layoutView(view, inContentView: contentView) } } + +public struct Layout: TableItemLayout { + public static func layoutView(_ view: UIView, inContentView contentView: UIView) { + Top.layoutView(view, inContentView: contentView) + Leading.layoutView(view, inContentView: contentView) + Bottom.layoutView(view, inContentView: contentView) + Trailing.layoutView(view, inContentView: contentView) + } +} diff --git a/FunctionalTableData/UIView+AutoLayout.swift b/FunctionalTableData/UIView+AutoLayout.swift index 882f2b5..a72c699 100644 --- a/FunctionalTableData/UIView+AutoLayout.swift +++ b/FunctionalTableData/UIView+AutoLayout.swift @@ -15,88 +15,4 @@ extension UIView { addSubview(view) } } - - func constraintsToFillView(_ otherView: UIView, respectingLayoutMargins: Bool = false) -> [NSLayoutConstraint] { - return constraintsToFillViewHorizontally(otherView, respectingLayoutMargins: respectingLayoutMargins) + constraintsToFillViewVertically(otherView, respectingLayoutMargins: respectingLayoutMargins) - } - - func constraintsToFillView(_ otherView: UIView, insetBy margins: UIEdgeInsets, respectingLayoutMargins: Bool = false) -> [NSLayoutConstraint] { - - if respectingLayoutMargins { - let layoutMarginsGuide = otherView.layoutMarginsGuide - - return [ - topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor, constant: margins.top), - bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor, constant: -margins.bottom), - leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor, constant: margins.left), - trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor, constant: -margins.right) - ] - } else { - return [ - topAnchor.constraint(equalTo: otherView.topAnchor, constant: margins.top), - bottomAnchor.constraint(equalTo: otherView.bottomAnchor, constant: -margins.bottom), - leadingAnchor.constraint(equalTo: otherView.leadingAnchor, constant: margins.left), - trailingAnchor.constraint(equalTo: otherView.trailingAnchor, constant: -margins.right) - ] - } - } - - func constraintsToFillViewVertically(_ otherView: UIView, respectingLayoutMargins: Bool = false) -> [NSLayoutConstraint] { - if respectingLayoutMargins { - let layoutMarginsGuide = otherView.layoutMarginsGuide - - return [ - topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor), - bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor) - ] - } else { - return [ - topAnchor.constraint(equalTo: otherView.topAnchor), - bottomAnchor.constraint(equalTo: otherView.bottomAnchor) - ] - } - } - - func constraintsToFillViewHorizontally(_ otherView: UIView, respectingLayoutMargins: Bool = false) -> [NSLayoutConstraint] { - if respectingLayoutMargins { - let layoutMarginsGuide = otherView.layoutMarginsGuide - - return [ - leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor), - trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor) - ] - } else { - return [ - leadingAnchor.constraint(equalTo: otherView.leadingAnchor), - trailingAnchor.constraint(equalTo: otherView.trailingAnchor) - ] - } - } - - func constraintsToBeCenteredInView(_ otherView: UIView) -> [NSLayoutConstraint] { - return [ - centerXAnchor.constraint(equalTo: otherView.centerXAnchor), - centerYAnchor.constraint(equalTo: otherView.centerYAnchor) - ] - } - - func constrainToFillView(_ otherView: UIView, respectingLayoutMargins: Bool = false) { - translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate(constraintsToFillView(otherView, respectingLayoutMargins: respectingLayoutMargins)) - } - - func constrainToFillViewVertically(_ otherView: UIView, respectingLayoutMargins: Bool = false) { - translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate(constraintsToFillViewVertically(otherView, respectingLayoutMargins: respectingLayoutMargins)) - } - - func constrainToFillViewHorizontally(_ otherView: UIView, respectingLayoutMargins: Bool = false) { - translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate(constraintsToFillViewHorizontally(otherView, respectingLayoutMargins: respectingLayoutMargins)) - } - - func constrainToBeCenteredInView(_ otherView: UIView) { - translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate(constraintsToBeCenteredInView(otherView)) - } } From 97b2682fdfbfbdc5c255b64776cd8ea177b458f8 Mon Sep 17 00:00:00 2001 From: Raul Riera Date: Fri, 25 May 2018 14:41:37 -0400 Subject: [PATCH 2/2] Fixed the comments --- FunctionalTableData/TableItemLayout.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FunctionalTableData/TableItemLayout.swift b/FunctionalTableData/TableItemLayout.swift index ba647d1..446a3d1 100644 --- a/FunctionalTableData/TableItemLayout.swift +++ b/FunctionalTableData/TableItemLayout.swift @@ -29,7 +29,7 @@ public struct ExplicitLayoutMarginsTableItemLayout: TableItemLayout { } public enum EdgeLayout { - @available(*, deprecated, message: "Use `Leading` and `EdgeLayout.Trailing` instead.") + @available(*, deprecated, message: "Use `EdgeLayout.Leading` and `EdgeLayout.Trailing` instead.") public struct Horizontal: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { Leading.layoutView(view, inContentView: contentView) @@ -112,7 +112,7 @@ public enum MarginsLayout { } } -@available(*, deprecated, message: "Use `Layout(Top:Leading:Bottom:Trailing)` instead.") +@available(*, deprecated, message: "Use `Layout` instead.") public struct CombinedLayout: TableItemLayout { public static func layoutView(_ view: UIView, inContentView contentView: UIView) { Horizontal.layoutView(view, inContentView: contentView)