|
| 1 | +import UIKit |
| 2 | + |
| 3 | +class ShiftingNavigationBarView: ShiftingTopView, Themeable { |
| 4 | + |
| 5 | + private let navigationItems: [UINavigationItem] |
| 6 | + private weak var popDelegate: UIViewController? // Navigation back actions will be forwarded to this view controller |
| 7 | + |
| 8 | + private var topConstraint: NSLayoutConstraint? |
| 9 | + |
| 10 | + private lazy var bar: UINavigationBar = { |
| 11 | + let bar = UINavigationBar() |
| 12 | + bar.translatesAutoresizingMaskIntoConstraints = false |
| 13 | + bar.delegate = self |
| 14 | + return bar |
| 15 | + }() |
| 16 | + |
| 17 | + init(shiftOrder: Int, navigationItems: [UINavigationItem], popDelegate: UIViewController) { |
| 18 | + self.navigationItems = navigationItems |
| 19 | + self.popDelegate = popDelegate |
| 20 | + super.init(shiftOrder: shiftOrder) |
| 21 | + } |
| 22 | + |
| 23 | + required init?(coder: NSCoder) { |
| 24 | + fatalError("init(coder:) has not been implemented") |
| 25 | + } |
| 26 | + |
| 27 | + override func setup() { |
| 28 | + super.setup() |
| 29 | + |
| 30 | + bar.setItems(navigationItems, animated: false) |
| 31 | + addSubview(bar) |
| 32 | + |
| 33 | + let top = bar.topAnchor.constraint(equalTo: topAnchor) |
| 34 | + let bottom = bottomAnchor.constraint(equalTo: bar.bottomAnchor) |
| 35 | + let leading = bar.leadingAnchor.constraint(equalTo: leadingAnchor) |
| 36 | + let trailing = trailingAnchor.constraint(equalTo: bar.trailingAnchor) |
| 37 | + |
| 38 | + NSLayoutConstraint.activate([ |
| 39 | + top, |
| 40 | + bottom, |
| 41 | + leading, |
| 42 | + trailing |
| 43 | + ]) |
| 44 | + |
| 45 | + self.topConstraint = top |
| 46 | + |
| 47 | + clipsToBounds = true |
| 48 | + } |
| 49 | + |
| 50 | + // MARK: Overrides |
| 51 | + |
| 52 | + override var contentHeight: CGFloat { |
| 53 | + return bar.frame.height |
| 54 | + } |
| 55 | + |
| 56 | + private var isFullyHidden: Bool { |
| 57 | + return -(topConstraint?.constant ?? 0) == contentHeight |
| 58 | + } |
| 59 | + |
| 60 | + override func shift(amount: CGFloat) -> ShiftingTopView.AmountShifted { |
| 61 | + |
| 62 | + // Only allow navigation bar to move just out of frame |
| 63 | + let limitedShiftAmount = max(0, min(amount, contentHeight)) |
| 64 | + |
| 65 | + // Shrink and fade |
| 66 | + let percent = limitedShiftAmount / contentHeight |
| 67 | + let barScaleTransform = CGAffineTransformMakeScale(1.0 - (percent/2), 1.0 - (percent/2)) |
| 68 | + for subview in self.bar.subviews { |
| 69 | + for subview in subview.subviews { |
| 70 | + subview.transform = barScaleTransform |
| 71 | + } |
| 72 | + } |
| 73 | + alpha = 1.0 - percent |
| 74 | + |
| 75 | + // Shift Y placement |
| 76 | + if (self.topConstraint?.constant ?? 0) != -limitedShiftAmount { |
| 77 | + self.topConstraint?.constant = -limitedShiftAmount |
| 78 | + } |
| 79 | + |
| 80 | + return limitedShiftAmount |
| 81 | + } |
| 82 | + |
| 83 | + // MARK: Themeable |
| 84 | + |
| 85 | + func apply(theme: Theme) { |
| 86 | + bar.setBackgroundImage(theme.navigationBarBackgroundImage, for: .default) |
| 87 | + bar.titleTextAttributes = theme.navigationBarTitleTextAttributes |
| 88 | + bar.isTranslucent = false |
| 89 | + bar.barTintColor = theme.colors.chromeBackground |
| 90 | + bar.shadowImage = theme.navigationBarShadowImage |
| 91 | + bar.tintColor = theme.colors.chromeText |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// MARK: Themeable |
| 96 | + |
| 97 | +extension ShiftingNavigationBarView: UINavigationBarDelegate { |
| 98 | + func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool { |
| 99 | + popDelegate?.navigationController?.popViewController(animated: true) |
| 100 | + return false |
| 101 | + } |
| 102 | + |
| 103 | + // Taken from NavigationBar.swift |
| 104 | + func navigationBar(_ navigationBar: UINavigationBar, didPop item: UINavigationItem) { |
| 105 | + // During iOS 14's long press to access back history, this function is called *after* the unneeded navigationItems have been popped off. |
| 106 | + // However, with our custom navBar the actual articleVC isn't changed. So we need to find the articleVC for the top navItem, and pop to it. |
| 107 | + // This should be in `shouldPop`, but as of iOS 14.0, `shouldPop` isn't called when long pressing a back button. Once this is fixed by Apple, |
| 108 | + // we should move this to `shouldPop` to improve animations. (Update: A bug tracker was filed w/ Apple, and this won't be fixed anytime soon. |
| 109 | + // Apple: "This is expected behavior. Due to side effects that many clients have in the shouldPop handler, we do not consult it when using the back |
| 110 | + // button menu. We instead recommend that you hide the back button when you wish to disallow popping past a particular point in the navigation stack.") |
| 111 | + if let topNavigationItem = navigationBar.items?.last, |
| 112 | + let navController = popDelegate?.navigationController, |
| 113 | + let tappedViewController = navController.viewControllers.first(where: {$0.navigationItem == topNavigationItem}) { |
| 114 | + popDelegate?.navigationController?.popToViewController(tappedViewController, animated: true) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments