From 5ec0fe52a792e96f45005721b7427cf2b06c7660 Mon Sep 17 00:00:00 2001 From: Jason Clark Date: Tue, 11 May 2021 11:52:24 -0400 Subject: [PATCH] Bugfix: Content Height Adjustment (#31) * Adjust alignment of top and bottom constraint to line up with safe area --- RPStackable.podspec | 2 +- Stackable/ScrollingStackView.swift | 31 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/RPStackable.podspec b/RPStackable.podspec index 95e192f..cc029f4 100644 --- a/RPStackable.podspec +++ b/RPStackable.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = 'RPStackable' s.module_name = "Stackable" - s.version = '0.1.4' + s.version = '0.1.5' s.summary = 'Stackable is a delightful and declarative set of utilities for UIStackView.' # This description is used to generate tags and improve search results. diff --git a/Stackable/ScrollingStackView.swift b/Stackable/ScrollingStackView.swift index ceedf92..2cb4363 100644 --- a/Stackable/ScrollingStackView.swift +++ b/Stackable/ScrollingStackView.swift @@ -35,29 +35,44 @@ open class ScrollingStackView: UIScrollView { return super.touchesShouldCancel(in: view) } + open override func safeAreaInsetsDidChange() { + super.safeAreaInsetsDidChange() + topSafeAreaConstraint?.constant = safeAreaInsets.top + bottomSafeAreaConstraint?.constant = safeAreaInsets.bottom + } + + var topSafeAreaConstraint: NSLayoutConstraint? + var bottomSafeAreaConstraint: NSLayoutConstraint? + /// A container used to enforce that the stack content stays at least the height of the frame. private let contentView = UIView() public init() { super.init(frame: .zero) + contentInsetAdjustmentBehavior = .never + addSubview(contentView) contentView.translatesAutoresizingMaskIntoConstraints = false contentView.clipsToBounds = false - + + topSafeAreaConstraint = contentView.topAnchor.constraint(equalTo: topAnchor, constant: safeAreaInsets.top) + topSafeAreaConstraint?.isActive = true + + bottomSafeAreaConstraint = contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: safeAreaInsets.bottom) + bottomSafeAreaConstraint?.isActive = true + NSLayoutConstraint.activate([ - contentView.topAnchor.constraint(equalTo: topAnchor), contentView.leadingAnchor.constraint(equalTo: leadingAnchor), contentView.trailingAnchor.constraint(equalTo: trailingAnchor), - contentView.bottomAnchor.constraint(equalTo: bottomAnchor), - - contentView.heightAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.heightAnchor), - contentView.widthAnchor.constraint(equalTo: widthAnchor), + + contentView.heightAnchor.constraint(greaterThanOrEqualTo: safeAreaLayoutGuide.heightAnchor), + contentView.widthAnchor.constraint(equalTo: frameLayoutGuide.widthAnchor), ]) - + contentView.addSubview(stackView) stackView.translatesAutoresizingMaskIntoConstraints = false - + NSLayoutConstraint.activate([ stackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), stackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),