summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2021-07-12 14:36:54 +0200
committerAndrej Mihajlov <and@mullvad.net>2021-07-13 14:13:07 +0200
commit42177437b417c58d3e949d15608928c01923c50e (patch)
tree0cb2e0b5bdb508c7e3ac709b76abcf68597ab2b5
parent1e57b8b374053cab5897cf0f5c619accb12ad33d (diff)
downloadmullvadvpn-42177437b417c58d3e949d15608928c01923c50e.tar.xz
mullvadvpn-42177437b417c58d3e949d15608928c01923c50e.zip
Migrate to UINavigationBarAppearance API
-rw-r--r--ios/MullvadVPN/CustomNavigationBar.swift82
-rw-r--r--ios/MullvadVPN/SelectLocationNavigationController.swift10
-rw-r--r--ios/MullvadVPN/SelectLocationViewController.swift2
3 files changed, 59 insertions, 35 deletions
diff --git a/ios/MullvadVPN/CustomNavigationBar.swift b/ios/MullvadVPN/CustomNavigationBar.swift
index 2c9be14f39..51a7655254 100644
--- a/ios/MullvadVPN/CustomNavigationBar.swift
+++ b/ios/MullvadVPN/CustomNavigationBar.swift
@@ -10,16 +10,17 @@ import UIKit
class CustomNavigationBar: UINavigationBar {
- private static let setupAppearanceOnce: Void = {
- let buttonAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [CustomNavigationBar.self])
- buttonAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 4, vertical: 0), for: .default)
+ private static let setupAppearanceForIOS12Once: Void = {
+ if #available(iOS 13, *) {
+ // no-op
+ } else {
+ let buttonAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [CustomNavigationBar.self])
+ buttonAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 4, vertical: 0), for: .default)
+ }
}()
- var prefersOpaqueBackground: Bool {
- didSet {
- setOpaqueBackgroundAppearance(prefersOpaqueBackground)
- }
- }
+ private let customBackIndicatorImage = UIImage(named: "IconBack")
+ private let customBackIndicatorTransitionMask = UIImage(named: "IconBackTransitionMask")
// Returns the distance from the title label to the bottom of navigation bar
var titleLabelBottomInset: CGFloat {
@@ -41,13 +42,7 @@ class CustomNavigationBar: UINavigationBar {
}
override init(frame: CGRect) {
- Self.setupAppearanceOnce
-
- if #available(iOS 13, *) {
- prefersOpaqueBackground = false
- } else {
- prefersOpaqueBackground = true
- }
+ Self.setupAppearanceForIOS12Once
super.init(frame: frame)
@@ -56,28 +51,61 @@ class CustomNavigationBar: UINavigationBar {
margins.right = UIMetrics.contentLayoutMargins.right
layoutMargins = margins
- backIndicatorImage = UIImage(named: "IconBack")
- backIndicatorTransitionMaskImage = UIImage(named: "IconBackTransitionMask")
-
- setOpaqueBackgroundAppearance(prefersOpaqueBackground)
+ setupNavigationBarAppearance()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
- private func setOpaqueBackgroundAppearance(_ flag: Bool) {
- if flag {
+ private func setupNavigationBarAppearance() {
+ backgroundColor = .secondaryColor
+ isTranslucent = false
+
+ if #available(iOS 13, *) {
+ standardAppearance = makeNavigationBarAppearance()
+ scrollEdgeAppearance = makeNavigationBarAppearance()
+ } else {
+ backIndicatorImage = customBackIndicatorImage
+ backIndicatorTransitionMaskImage = customBackIndicatorTransitionMask
barTintColor = .secondaryColor
- backgroundColor = .secondaryColor
+ titleTextAttributes = [.foregroundColor: UIColor.white]
shadowImage = UIImage()
- isTranslucent = false
+ }
+ }
+
+ @available(iOS 13, *)
+ private func makeNavigationBarAppearance() -> UINavigationBarAppearance {
+ let textAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.white]
+
+ let navigationBarAppearance = UINavigationBarAppearance()
+ navigationBarAppearance.configureWithTransparentBackground()
+ navigationBarAppearance.titleTextAttributes = textAttributes
+ navigationBarAppearance.largeTitleTextAttributes = textAttributes
+
+ let plainBarButtonAppearance = UIBarButtonItemAppearance(style: .plain)
+ plainBarButtonAppearance.normal.titleTextAttributes = textAttributes
+
+ let doneBarButtonAppearance = UIBarButtonItemAppearance(style: .done)
+ doneBarButtonAppearance.normal.titleTextAttributes = textAttributes
+
+ let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
+ backButtonAppearance.normal.titleTextAttributes = textAttributes
+ backButtonAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 4, vertical: 0)
+
+ navigationBarAppearance.buttonAppearance = plainBarButtonAppearance
+ navigationBarAppearance.doneButtonAppearance = doneBarButtonAppearance
+ navigationBarAppearance.backButtonAppearance = backButtonAppearance
+
+ if #available(iOS 14, *) {
+ navigationBarAppearance.setBackIndicatorImage(customBackIndicatorImage, transitionMaskImage: customBackIndicatorTransitionMask)
} else {
- barTintColor = nil
- backgroundColor = nil
- shadowImage = nil
- isTranslucent = true
+ // Bug: on iOS 13 setBackIndicatorImage accepts parameters in backward order
+ // https://stackoverflow.com/a/58171229/351305
+ navigationBarAppearance.setBackIndicatorImage(customBackIndicatorTransitionMask, transitionMaskImage: customBackIndicatorImage)
}
+
+ return navigationBarAppearance
}
}
diff --git a/ios/MullvadVPN/SelectLocationNavigationController.swift b/ios/MullvadVPN/SelectLocationNavigationController.swift
index 13da20e934..46df46698d 100644
--- a/ios/MullvadVPN/SelectLocationNavigationController.swift
+++ b/ios/MullvadVPN/SelectLocationNavigationController.swift
@@ -14,18 +14,12 @@ class SelectLocationNavigationController: UINavigationController {
init(contentController: SelectLocationViewController) {
super.init(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
- navigationBar.barStyle = .black
- navigationBar.tintColor = .white
- navigationBar.prefersLargeTitles = false
-
- (navigationBar as? CustomNavigationBar)?.prefersOpaqueBackground = true
-
self.viewControllers = [contentController]
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
- // This override has to exist to prevent crash on iOS 12 where `UINavigationController`
- // calls `self.init(nibName:bundle:)` internally.
+ // This initializer exists to prevent crash on iOS 12.
+ // See: https://stackoverflow.com/a/38335090/351305
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
diff --git a/ios/MullvadVPN/SelectLocationViewController.swift b/ios/MullvadVPN/SelectLocationViewController.swift
index 2e17e44a15..a5cffcc452 100644
--- a/ios/MullvadVPN/SelectLocationViewController.swift
+++ b/ios/MullvadVPN/SelectLocationViewController.swift
@@ -52,6 +52,8 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
+ navigationItem.largeTitleDisplayMode = .never
+
let tableView = UITableView(frame: view.bounds, style: .plain)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = .clear