diff --git a/Example/AloeStackViewExample.xcodeproj/project.pbxproj b/Example/AloeStackViewExample.xcodeproj/project.pbxproj index 798e405..bda3972 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..15a80d8 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's 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 new file mode 100644 index 0000000..b320fee --- /dev/null +++ b/Example/AloeStackViewExample/ViewControllers/PreserveLayoutMarginsViewController.swift @@ -0,0 +1,66 @@ +// 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 + +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" + } + + private func setUpStackView() { + // Setting this property to true will constrain the stack view content within the layout margins of the superview + stackView.preservesSuperviewLayoutMargins = true + + // 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 + } + + private func setUpRows() { + setUpDescriptionRow() + setUpAnotherRow() + } + + 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) + } +} diff --git a/Sources/AloeStackView/AloeStackView.swift b/Sources/AloeStackView/AloeStackView.swift index 284ed2d..aefab8e 100644 --- a/Sources/AloeStackView/AloeStackView.swift +++ b/Sources/AloeStackView/AloeStackView.swift @@ -328,6 +328,18 @@ 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 explicitly. + 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