Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit c14268e

Browse files
author
Oscar Apeland
committed
Improved large navigation bar handling
1 parent 8179736 commit c14268e

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

SwipeableViewController.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
08A9E2F61F9E356C000B2729 /* SwipeableItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2E71F9E356C000B2729 /* SwipeableItem.swift */; };
1313
08A9E2F71F9E356C000B2729 /* SwipeableCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2E81F9E356C000B2729 /* SwipeableCollectionView.swift */; };
1414
08A9E2F81F9E356C000B2729 /* SwipeableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2E91F9E356C000B2729 /* SwipeableCell.swift */; };
15-
08A9E2F91F9E356C000B2729 /* SwipeableNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2EA1F9E356C000B2729 /* SwipeableNavigationController.swift */; };
1615
08A9E2FA1F9E356C000B2729 /* SwipeableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2EB1F9E356C000B2729 /* SwipeableViewController.swift */; };
1716
08A9E2FB1F9E356C000B2729 /* SwipeableCollectionViewFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2EC1F9E356C000B2729 /* SwipeableCollectionViewFlowLayout.swift */; };
1817
08A9E2FC1F9E356C000B2729 /* SwipeableCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 08A9E2ED1F9E356C000B2729 /* SwipeableCell.xib */; };
1918
08A9E2FD1F9E356C000B2729 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 08A9E2EE1F9E356C000B2729 /* Assets.xcassets */; };
2019
08A9E2FE1F9E356C000B2729 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 08A9E2EF1F9E356C000B2729 /* LaunchScreen.storyboard */; };
2120
08A9E2FF1F9E356C000B2729 /* ExampleSwipeableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2F11F9E356C000B2729 /* ExampleSwipeableViewController.swift */; };
2221
08A9E3001F9E356C000B2729 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E2F21F9E356C000B2729 /* AppDelegate.swift */; };
22+
08D0565D1FA723CD00300DBF /* SwipeableNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08D0565C1FA723CD00300DBF /* SwipeableNavigationController.swift */; };
2323
/* End PBXBuildFile section */
2424

2525
/* Begin PBXFileReference section */
@@ -29,7 +29,6 @@
2929
08A9E2E71F9E356C000B2729 /* SwipeableItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwipeableItem.swift; sourceTree = "<group>"; };
3030
08A9E2E81F9E356C000B2729 /* SwipeableCollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwipeableCollectionView.swift; sourceTree = "<group>"; };
3131
08A9E2E91F9E356C000B2729 /* SwipeableCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwipeableCell.swift; sourceTree = "<group>"; };
32-
08A9E2EA1F9E356C000B2729 /* SwipeableNavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwipeableNavigationController.swift; sourceTree = "<group>"; };
3332
08A9E2EB1F9E356C000B2729 /* SwipeableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwipeableViewController.swift; sourceTree = "<group>"; };
3433
08A9E2EC1F9E356C000B2729 /* SwipeableCollectionViewFlowLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwipeableCollectionViewFlowLayout.swift; sourceTree = "<group>"; };
3534
08A9E2ED1F9E356C000B2729 /* SwipeableCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SwipeableCell.xib; sourceTree = "<group>"; };
@@ -38,6 +37,7 @@
3837
08A9E2F11F9E356C000B2729 /* ExampleSwipeableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleSwipeableViewController.swift; sourceTree = "<group>"; };
3938
08A9E2F21F9E356C000B2729 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4039
08A9E2F31F9E356C000B2729 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
40+
08D0565C1FA723CD00300DBF /* SwipeableNavigationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwipeableNavigationController.swift; sourceTree = "<group>"; };
4141
/* End PBXFileReference section */
4242

4343
/* Begin PBXFrameworksBuildPhase section */
@@ -84,11 +84,11 @@
8484
08A9E2E51F9E356C000B2729 /* Source */ = {
8585
isa = PBXGroup;
8686
children = (
87+
08D0565C1FA723CD00300DBF /* SwipeableNavigationController.swift */,
8788
08A9E2E61F9E356C000B2729 /* SwipeableNavigationBar.swift */,
8889
08A9E2E71F9E356C000B2729 /* SwipeableItem.swift */,
8990
08A9E2E81F9E356C000B2729 /* SwipeableCollectionView.swift */,
9091
08A9E2E91F9E356C000B2729 /* SwipeableCell.swift */,
91-
08A9E2EA1F9E356C000B2729 /* SwipeableNavigationController.swift */,
9292
08A9E2EB1F9E356C000B2729 /* SwipeableViewController.swift */,
9393
08A9E2EC1F9E356C000B2729 /* SwipeableCollectionViewFlowLayout.swift */,
9494
08A9E2ED1F9E356C000B2729 /* SwipeableCell.xib */,
@@ -168,10 +168,10 @@
168168
isa = PBXSourcesBuildPhase;
169169
buildActionMask = 2147483647;
170170
files = (
171+
08D0565D1FA723CD00300DBF /* SwipeableNavigationController.swift in Sources */,
171172
08A9E2F81F9E356C000B2729 /* SwipeableCell.swift in Sources */,
172173
08A9E2FF1F9E356C000B2729 /* ExampleSwipeableViewController.swift in Sources */,
173174
08A9E2F51F9E356C000B2729 /* SwipeableNavigationBar.swift in Sources */,
174-
08A9E2F91F9E356C000B2729 /* SwipeableNavigationController.swift in Sources */,
175175
08A9E2FB1F9E356C000B2729 /* SwipeableCollectionViewFlowLayout.swift in Sources */,
176176
08A9E2F61F9E356C000B2729 /* SwipeableItem.swift in Sources */,
177177
08A9E2F41F9E356C000B2729 /* ExampleViewController.swift in Sources */,

SwipeableViewController/ExampleSwipeableViewController.swift

+2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class ExampleSwipeableViewController: SwipeableViewController {
1717
override func viewDidLoad() {
1818
super.viewDidLoad()
1919
if #available(iOS 11.0, *) {
20+
navigationItem.largeTitleDisplayMode = .always
2021
navigationItem.searchController = searchController
2122
} else {
2223
navigationItem.titleView = searchController.searchBar
2324
}
25+
2426
definesPresentationContext = true
2527
}
2628
}

SwipeableViewController/Source/SwipeableNavigationBar.swift

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@
99
import UIKit
1010

1111
open class SwipeableNavigationBar: UINavigationBar {
12+
public override init(frame: CGRect) {
13+
super.init(frame: frame)
14+
setup()
15+
}
16+
17+
public required init?(coder aDecoder: NSCoder) {
18+
super.init(coder: aDecoder)
19+
setup()
20+
}
21+
22+
private func setup() {
23+
if #available(iOS 11.0, *) {
24+
largeTitleTextAttributes = [.foregroundColor: UIColor.clear]
25+
prefersLargeTitles = true
26+
}
27+
}
28+
1229
// MARK: Properties
1330
lazy var largeTitleView: UIView? = {
14-
return self.subviews.first { String(describing: type(of: $0)) == "_UINavigationBarLargeTitleView" }
31+
return self.subviews.first {
32+
String(describing: type(of: $0)) == "_UINavigationBarLargeTitleView"
33+
}
1534
}()
1635

1736
var largeTitleLabel: UILabel? {

SwipeableViewController/Source/SwipeableNavigationController.swift

+21-14
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,40 @@
22
// SwipeableNavigationController.swift
33
// SwipingViewController
44
//
5-
// Created by Oscar Apeland on 12.10.2017.
5+
// Created by Oscar Apeland on 30.10.2017.
66
// Copyright © 2017 Tise. All rights reserved.
77
//
88

9+
import Foundation
910
import UIKit
1011

11-
open class SwipeableNavigationController: UINavigationController {
12-
override open func viewDidLoad() {
12+
class SwipeableNavigationController: UINavigationController {
13+
override func viewDidLoad() {
1314
super.viewDidLoad()
1415
delegate = self
1516
}
1617
}
1718

1819
extension SwipeableNavigationController: UINavigationControllerDelegate {
19-
open func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
20-
if #available(iOS 11.0, *) {
21-
navigationController.navigationBar.prefersLargeTitles = viewController is SwipeableViewController
22-
}
23-
24-
if let largeTitleLabel = (navigationBar as? SwipeableNavigationBar)?.largeTitleLabel {
25-
navigationBar.sendSubview(toBack: largeTitleLabel)
26-
}
20+
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
21+
updateFor(viewController: viewController)
22+
}
23+
24+
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
25+
updateFor(viewController: viewController)
2726
}
2827

29-
open func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
30-
if #available(iOS 11.0, *) {
31-
navigationController.navigationBar.prefersLargeTitles = viewController is SwipeableViewController
28+
func updateFor(viewController: UIViewController) {
29+
let isSwipeable = viewController is SwipeableViewController
30+
31+
if #available(iOS 11, *) {
32+
viewController.navigationItem.largeTitleDisplayMode = isSwipeable ? .always : .never
33+
}
34+
35+
if #available(iOS 11, *), let collectionView = (navigationBar as? SwipeableNavigationBar)?.collectionView {
36+
collectionView.isHidden = !isSwipeable
37+
} else if let collectionView = (viewController as? SwipeableViewController)?.collectionView {
38+
collectionView.isHidden = !isSwipeable
3239
}
3340
}
3441
}

SwipeableViewController/Source/SwipeableViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ open class SwipeableViewController: UIViewController {
6363

6464
// Setup - Navigation bar
6565
navigationItem.title = initialItem.title
66+
6667
if #available(iOS 11.0, *) {
67-
navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.clear]
68+
navigationItem.largeTitleDisplayMode = .always
6869
}
6970

7071
// Setup - View

0 commit comments

Comments
 (0)