Skip to content

Commit

Permalink
Merge pull request #1 from finyard/fix-layout-pass-cherry-pick-from-4.0
Browse files Browse the repository at this point in the history
Merge pull request rechsteiner#680 from rechsteiner/fix-layout-issues
  • Loading branch information
mlee-finyard authored May 22, 2024
2 parents 6e955f1 + 974f536 commit 2f39723
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 29 deletions.
29 changes: 29 additions & 0 deletions Example/ExamplesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,32 @@ final class ExamplesViewController: UITableViewController {
dismiss(animated: true)
}
}

final class NavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

override func viewDidLoad() {
super.viewDidLoad()
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemBlue
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navigationBar.tintColor = .white
navigationBar.standardAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
}

// For debugging purposes. Adds a hook to push a new view
// controller to debug navigation controller related issues.
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
let viewController = UIViewController()
viewController.title = "Page"
viewController.view.backgroundColor = .white
pushViewController(viewController, animated: true)
}
}
}
30 changes: 19 additions & 11 deletions Example/Resources/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="q4m-KO-mBR">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="ti2-Oh-iiB">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
Expand All @@ -9,16 +9,21 @@
</dependencies>
<scenes>
<!--Navigation Controller-->
<scene sceneID="UJ4-9T-uzW">
<scene sceneID="KMO-vS-UdG">
<objects>
<navigationController extendedLayoutIncludesOpaqueBars="YES" id="q4m-KO-mBR" sceneMemberID="viewController">
<extendedEdge key="edgesForExtendedLayout" bottom="YES"/>
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" barStyle="black" translucent="NO" prompted="NO"/>
<navigationBar key="navigationBar" contentMode="scaleToFill" barStyle="black" translucent="NO" id="Tuq-Hd-LFa">
<navigationController id="ti2-Oh-iiB" customClass="NavigationController" customModule="Example" customModuleProvider="target" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="Qw0-yq-kTw">
<rect key="frame" x="0.0" y="48" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" systemColor="linkColor"/>
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="barTintColor" systemColor="linkColor"/>
<color key="barTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<textAttributes key="titleTextAttributes">
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</textAttributes>
<textAttributes key="largeTitleTextAttributes">
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</textAttributes>
<navigationBarAppearance key="standardAppearance">
<color key="backgroundColor" systemColor="linkColor"/>
<textAttributes key="titleTextAttributes">
Expand All @@ -27,6 +32,9 @@
</navigationBarAppearance>
<navigationBarAppearance key="compactAppearance">
<color key="backgroundColor" systemColor="linkColor"/>
<textAttributes key="titleTextAttributes">
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</textAttributes>
</navigationBarAppearance>
<navigationBarAppearance key="scrollEdgeAppearance">
<color key="backgroundColor" systemColor="linkColor"/>
Expand All @@ -36,19 +44,19 @@
</navigationBarAppearance>
</navigationBar>
<connections>
<segue destination="zQo-ny-4EL" kind="relationship" relationship="rootViewController" id="EWy-YM-lkZ"/>
<segue destination="zQo-ny-4EL" kind="relationship" relationship="rootViewController" id="U3Y-LD-KMp"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="6Qm-qq-xBM" userLabel="First Responder" sceneMemberID="firstResponder"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="0QX-Wb-stH" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-483" y="322"/>
<point key="canvasLocation" x="-536" y="321"/>
</scene>
<!--Examples-->
<scene sceneID="Skl-JE-8hv">
<objects>
<tableViewController id="zQo-ny-4EL" customClass="ExamplesViewController" customModule="Example" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="qJk-Tf-P33">
<rect key="frame" x="0.0" y="0.0" width="414" height="804"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes>
Expand Down
25 changes: 11 additions & 14 deletions Parchment/Classes/PageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ public final class PageViewController: UIViewController {
public private(set) lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.isPagingEnabled = true
scrollView.autoresizingMask = [
.flexibleTopMargin,
.flexibleRightMargin,
.flexibleBottomMargin,
.flexibleLeftMargin,
]
scrollView.scrollsToTop = false
scrollView.bounces = true
scrollView.translatesAutoresizingMaskIntoConstraints = true
Expand Down Expand Up @@ -140,12 +134,19 @@ public final class PageViewController: UIViewController {
view.addSubview(scrollView)
scrollView.delegate = self
scrollView.contentInsetAdjustmentBehavior = .never
scrollView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
])
}

public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
scrollView.frame = view.bounds
manager.viewWillLayoutSubviews()
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
manager.viewDidLayoutSubviews()
}

public override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -302,10 +303,6 @@ extension PageViewController: PageViewManagerDelegate {
func layoutViews(for viewControllers: [UIViewController], keepContentOffset: Bool) {
let viewControllers = isRightToLeft ? viewControllers.reversed() : viewControllers

// Need to trigger a layout here to ensure that the scroll view
// bounds is updated before we use its frame for calculations.
view.layoutIfNeeded()

for (index, viewController) in viewControllers.enumerated() {
switch options.contentNavigationOrientation {
case .horizontal:
Expand Down
6 changes: 3 additions & 3 deletions Parchment/Classes/PageViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ final class PageViewManager {
}
}

func viewWillLayoutSubviews() {
layoutsViews()
func viewDidLayoutSubviews() {
layoutsViews(keepContentOffset: false)
}

func viewWillAppear(_ animated: Bool) {
Expand All @@ -170,7 +170,7 @@ final class PageViewManager {

switch state {
case .center, .first, .last, .single:
layoutsViews()
layoutsViews(keepContentOffset: false)
case .empty:
break
}
Expand Down
18 changes: 17 additions & 1 deletion Parchment/Classes/PagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ open class PagingViewController:

private let pagingController: PagingController
private var didLayoutSubviews: Bool = false
private var didTransitionSize: Bool = false

private var pagingView: PagingView {
return view as! PagingView
Expand Down Expand Up @@ -488,6 +489,20 @@ open class PagingViewController:
configureContentInteraction()
}

open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #unavailable(iOS 16), didTransitionSize {
view.layoutIfNeeded()
pagingController.transitionSize()
}
}

open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
didTransitionSize = false
didLayoutSubviews = false
}

open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

Expand All @@ -502,9 +517,10 @@ open class PagingViewController:

open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
didTransitionSize = true
coordinator.animate(alongsideTransition: { _ in
self.pagingController.transitionSize()
}, completion: nil)
})
}

/// Register cell class for paging cell
Expand Down

0 comments on commit 2f39723

Please sign in to comment.