From 269da9b74283761cad7a97f3054ebb923bfa9a79 Mon Sep 17 00:00:00 2001 From: alexbredy Date: Fri, 1 Feb 2019 13:57:17 +0200 Subject: [PATCH 1/5] Add the possibility to wrap the stack view within the superview's layout margins --- .../PreserveLayoutMarginsViewController.swift | 30 +++++++++++++++++++ Sources/AloeStackView/AloeStackView.swift | 11 +++++++ 2 files changed, 41 insertions(+) create mode 100644 Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift diff --git a/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift b/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift new file mode 100644 index 0000000..65e96f1 --- /dev/null +++ b/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift @@ -0,0 +1,30 @@ +// +// PreserveLayoutMarginsViewController.swift +// AloeStackViewExample +// +// Created by Alexander Bredy on 01/02/2019. +// Copyright © 2019 Airbnb, Inc. All rights reserved. +// + +import UIKit + +class PreserveLayoutMarginsViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // Get the new view controller using segue.destination. + // Pass the selected object to the new view controller. + } + */ + +} diff --git a/Sources/AloeStackView/AloeStackView.swift b/Sources/AloeStackView/AloeStackView.swift index ecc2748..61c5eae 100644 --- a/Sources/AloeStackView/AloeStackView.swift +++ b/Sources/AloeStackView/AloeStackView.swift @@ -304,6 +304,17 @@ open class AloeStackView: UIScrollView { } } + /// Set whether the layout margins of the superview should be included + /// This comes in handy as iPhones and iPads can have different layout margins + /// and convenient as it automatically provide an inset to all the rows + /// without having to set it explicitely + open override var preservesSuperviewLayoutMargins: Bool { + didSet { + stackView.preservesSuperviewLayoutMargins = preservesSuperviewLayoutMargins + stackView.isLayoutMarginsRelativeArrangement = preservesSuperviewLayoutMargins + } + } + /// Specifies the default inset of row separators. /// /// Only left and right insets are honored. This inset will be used for any new row that is added From 6ee87258910ee001fbe524248a23518481fbd030 Mon Sep 17 00:00:00 2001 From: alexbredy Date: Fri, 1 Feb 2019 13:57:42 +0200 Subject: [PATCH 2/5] Add an example related to the superview layout margins --- .../project.pbxproj | 4 + .../ViewControllers/MainViewController.swift | 16 ++++ .../PreserveLayoutMarginsViewController.swift | 74 ++++++++++++++----- 3 files changed, 75 insertions(+), 19 deletions(-) diff --git a/Example/AloeStackViewExample.xcodeproj/project.pbxproj b/Example/AloeStackViewExample.xcodeproj/project.pbxproj index 28d24bf..53258a1 100644 --- a/Example/AloeStackViewExample.xcodeproj/project.pbxproj +++ b/Example/AloeStackViewExample.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 3EE7174D22045E54006E6F75 /* PreserveLayoutMarginsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EE7174C22045E54006E6F75 /* PreserveLayoutMarginsViewController.swift */; }; A7497A9121746D9700BB692A /* AloeStackView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A7497A9021746D9700BB692A /* AloeStackView.framework */; }; A7497A932174725700BB692A /* SwitchRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7497A922174725700BB692A /* SwitchRowView.swift */; }; A7497A9721747DD600BB692A /* PhotoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7497A9621747DD600BB692A /* PhotoViewController.swift */; }; @@ -18,6 +19,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 3EE7174C22045E54006E6F75 /* PreserveLayoutMarginsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreserveLayoutMarginsViewController.swift; sourceTree = ""; }; A7420FAB21715DF500F2A343 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A7497A9021746D9700BB692A /* AloeStackView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = AloeStackView.framework; sourceTree = BUILT_PRODUCTS_DIR; }; A7497A922174725700BB692A /* SwitchRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwitchRowView.swift; sourceTree = ""; }; @@ -47,6 +49,7 @@ children = ( A74F70BA217155FC0054AA18 /* MainViewController.swift */, A7497A9621747DD600BB692A /* PhotoViewController.swift */, + 3EE7174C22045E54006E6F75 /* PreserveLayoutMarginsViewController.swift */, ); path = ViewControllers; sourceTree = ""; @@ -204,6 +207,7 @@ A7497A9721747DD600BB692A /* PhotoViewController.swift in Sources */, A74F70B9217155FC0054AA18 /* AppDelegate.swift in Sources */, A7497A9921755AD100BB692A /* ExpandingRowView.swift in Sources */, + 3EE7174D22045E54006E6F75 /* PreserveLayoutMarginsViewController.swift in Sources */, A7497A932174725700BB692A /* SwitchRowView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/AloeStackViewExample/ViewControllers/MainViewController.swift b/Example/AloeStackViewExample/ViewControllers/MainViewController.swift index 56ff0b7..8c0f894 100644 --- a/Example/AloeStackViewExample/ViewControllers/MainViewController.swift +++ b/Example/AloeStackViewExample/ViewControllers/MainViewController.swift @@ -43,6 +43,7 @@ public class MainViewController: AloeStackViewController { setUpHiddenRows() setUpExpandingRowView() setUpPhotoRow() + setUpPreserveLayoutMarginsRow() } private func setUpDescriptionRow() { @@ -137,4 +138,19 @@ public class MainViewController: AloeStackViewController { } } + private func setUpPreserveLayoutMarginsRow() { + let label = UILabel() + label.font = UIFont.preferredFont(forTextStyle: .body) + label.numberOfLines = 0 + label.text = "Tap here to view an AloeStackView constrained within its superview layout margins" + label.isUserInteractionEnabled = true + + stackView.addRow(label) + stackView.setTapHandler(forRow: label) { [weak self] _ in + guard let self = self else { return } + let vc = PreserveLayoutMarginsViewController() + self.navigationController?.pushViewController(vc, animated: true) + } + } + } diff --git a/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift b/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift index 65e96f1..b320fee 100644 --- a/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift +++ b/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift @@ -1,30 +1,66 @@ -// -// PreserveLayoutMarginsViewController.swift -// AloeStackViewExample -// // Created by Alexander Bredy on 01/02/2019. // Copyright © 2019 Airbnb, Inc. All rights reserved. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import AloeStackView import UIKit -class PreserveLayoutMarginsViewController: UIViewController { +public class PreserveLayoutMarginsViewController: AloeStackViewController { + + // MARK: Public + + public override func viewDidLoad() { + super.viewDidLoad() + setUpSelf() + setUpStackView() + setUpRows() + } + + // MARK: Private + + private func setUpSelf() { + title = "Preserve superview layout margins" + } - override func viewDidLoad() { - super.viewDidLoad() + private func setUpStackView() { + // Setting this property to true will constrain the stack view content within the layout margins of the superview + stackView.preservesSuperviewLayoutMargins = true - // Do any additional setup after loading the view. - } - + // Setting separator and row insets on this screen will add additional margins to the ones set by the + // superview. + stackView.separatorInset = .zero + stackView.rowInset = .zero + } - /* - // MARK: - Navigation + private func setUpRows() { + setUpDescriptionRow() + setUpAnotherRow() + } - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destination. - // Pass the selected object to the new view controller. - } - */ + private func setUpDescriptionRow() { + let label = UILabel() + label.font = UIFont.preferredFont(forTextStyle: .body) + label.numberOfLines = 0 + label.text = "The margins of the AloeStackView in this screen include the ones from the superview layout margins." + stackView.addRow(label) + } + private func setUpAnotherRow() { + let label = UILabel() + label.font = UIFont.preferredFont(forTextStyle: .body) + label.numberOfLines = 0 + label.text = "Check out this ViewController on both iPhone and iPad to see the margins difference!" + stackView.addRow(label) + } } From d0dfcda8b6fb22dcc9b18a36be6665bdda6d8221 Mon Sep 17 00:00:00 2001 From: alexbredy Date: Fri, 1 Feb 2019 14:09:05 +0200 Subject: [PATCH 3/5] format comment --- Sources/AloeStackView/AloeStackView.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/AloeStackView/AloeStackView.swift b/Sources/AloeStackView/AloeStackView.swift index 61c5eae..e9807a6 100644 --- a/Sources/AloeStackView/AloeStackView.swift +++ b/Sources/AloeStackView/AloeStackView.swift @@ -304,10 +304,11 @@ open class AloeStackView: UIScrollView { } } - /// Set whether the layout margins of the superview should be included + /// Set whether the layout margins of the superview should be included. + /// /// This comes in handy as iPhones and iPads can have different layout margins /// and convenient as it automatically provide an inset to all the rows - /// without having to set it explicitely + /// without having to set it explicitely. open override var preservesSuperviewLayoutMargins: Bool { didSet { stackView.preservesSuperviewLayoutMargins = preservesSuperviewLayoutMargins From e7d0ade4baf4077d27984145b453676126b508de Mon Sep 17 00:00:00 2001 From: alexbredy Date: Fri, 1 Feb 2019 14:12:46 +0200 Subject: [PATCH 4/5] edit copy of mainvc --- .../ViewControllers/MainViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Example/AloeStackViewExample/ViewControllers/MainViewController.swift b/Example/AloeStackViewExample/ViewControllers/MainViewController.swift index 8c0f894..15a80d8 100644 --- a/Example/AloeStackViewExample/ViewControllers/MainViewController.swift +++ b/Example/AloeStackViewExample/ViewControllers/MainViewController.swift @@ -142,7 +142,7 @@ public class MainViewController: AloeStackViewController { let label = UILabel() label.font = UIFont.preferredFont(forTextStyle: .body) label.numberOfLines = 0 - label.text = "Tap here to view an AloeStackView constrained within its superview layout margins" + label.text = "Tap here to view an AloeStackView constrained within its superview's layout margins." label.isUserInteractionEnabled = true stackView.addRow(label) From ac7f07faf41b054179b6f4b72e03fa43113cc07b Mon Sep 17 00:00:00 2001 From: Alexander Bredy Date: Mon, 3 Jun 2019 15:45:48 +0100 Subject: [PATCH 5/5] Fix typo in AloeStackView.swift Co-Authored-By: Yasuharu Yanamura --- Sources/AloeStackView/AloeStackView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AloeStackView/AloeStackView.swift b/Sources/AloeStackView/AloeStackView.swift index e9807a6..0e7e647 100644 --- a/Sources/AloeStackView/AloeStackView.swift +++ b/Sources/AloeStackView/AloeStackView.swift @@ -308,7 +308,7 @@ open class AloeStackView: UIScrollView { /// /// This comes in handy as iPhones and iPads can have different layout margins /// and convenient as it automatically provide an inset to all the rows - /// without having to set it explicitely. + /// without having to set it explicitly. open override var preservesSuperviewLayoutMargins: Bool { didSet { stackView.preservesSuperviewLayoutMargins = preservesSuperviewLayoutMargins