Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
CPD-14518: Default to not nest arranged views in container
  • Loading branch information
edrutter-je committed Jul 12, 2018
1 parent 48e5b48 commit fd76423
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
@@ -1,5 +1,5 @@
PODS:
- ScrollingStackViewController (3.1.2)
- ScrollingStackViewController (4.0.0)

DEPENDENCIES:
- ScrollingStackViewController (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ScrollingStackViewController: da1156942456d39a23313c6bd36d9f9bf01142ea
ScrollingStackViewController: c87977311c6e8b0c4d32fcf9530dd60250d9c470

PODFILE CHECKSUM: 1107023c97aaa6562f2eb38595e37b1267bba2b1

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ScrollingStackViewController.podspec
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'ScrollingStackViewController'
s.version = '3.1.2'
s.version = '4.0.0'
s.summary = 'A view controller that uses root views of child view controllers as views in a UIStackView.'

s.description = <<-DESC
Expand Down
Expand Up @@ -138,15 +138,15 @@ open class ScrollingStackViewController: UIViewController {
insert(viewController: viewController)
}

open func add(viewController: UIViewController, edgeInsets: UIEdgeInsets) {
open func add(viewController: UIViewController, edgeInsets: UIEdgeInsets? = nil) {
insert(viewController: viewController, edgeInsets: edgeInsets)
}

open func insert(viewController: UIViewController, at index: Int) {
insert(viewController: viewController, at: .index(index))
}

open func insert(viewController: UIViewController, edgeInsets: UIEdgeInsets = .zero, at position: Position = .end) {
open func insert(viewController: UIViewController, edgeInsets: UIEdgeInsets? = nil, at position: Position = .end) {
var insertionIndex: Int?

switch position {
Expand Down Expand Up @@ -177,35 +177,37 @@ open class ScrollingStackViewController: UIViewController {
insert(viewController: viewController, edgeInsets: edgeInsets, at: insertionIndex ?? childViewControllers.count)
}

open func insert(viewController: UIViewController, edgeInsets: UIEdgeInsets, at index: Int) {
open func insert(viewController: UIViewController, edgeInsets: UIEdgeInsets?, at index: Int) {

addChildViewController(viewController)
viewController.didMove(toParentViewController: self)

let childView: UIView = viewController.view
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
childView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(childView)

let constraints = [
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: edgeInsets.top),
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: edgeInsets.left),
containerView.bottomAnchor.constraint(equalTo: childView.bottomAnchor, constant: edgeInsets.bottom),
containerView.trailingAnchor.constraint(equalTo: childView.trailingAnchor, constant: edgeInsets.right),
]

NSLayoutConstraint.activate(constraints)

stackView.insertArrangedSubview(containerView, at: index)
if let edgeInsets = edgeInsets {

let childView: UIView = viewController.view
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
childView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(childView)

let constraints = [
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: edgeInsets.top),
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: edgeInsets.left),
containerView.bottomAnchor.constraint(equalTo: childView.bottomAnchor, constant: edgeInsets.bottom),
containerView.trailingAnchor.constraint(equalTo: childView.trailingAnchor, constant: edgeInsets.right),
]

NSLayoutConstraint.activate(constraints)
stackView.insertArrangedSubview(containerView, at: index)

} else {
stackView.insertArrangedSubview(viewController.view, at: index)
}
}

open func remove(viewController: UIViewController) {

if let index = arrangedViewOrContainerIndex(for: viewController.view) {
let viewToRemove = stackView.arrangedSubviews[index]
stackView.removeArrangedSubview(viewToRemove)
}
guard let arrangedView = arrangedView(for: viewController) else { return }
stackView.removeArrangedSubview(arrangedView)

viewController.willMove(toParentViewController: nil)
viewController.removeFromParentViewController()
Expand Down

0 comments on commit fd76423

Please sign in to comment.