Set global default back button display mode for all UIViewController instances
Asked Answered
H

2

3

iOS 14 introduced configurable Back button mode. Eg. you can have "Back" label text on the button, but in the compact history menu you can still see the proper title of previous controllers.

I'm looking for a easy, pleasant way to configure the default mode, so all UIViewController instances during runtime will have default mode set, like UINavigationItemBackButtonDisplayModeGeneric

I wonder if there is a way to do that without subclassing UIViewController or remember to always configure manually every instance of UIViewController (via viewController.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeGeneric).

Any handy method which does not require extensive refactoring of hundreds of UIViewController instances greatly appreciated!

Honeycomb answered 5/10, 2020 at 12:49 Comment(0)
O
1

Without subclassing I think is not possible since navigationItem requires an instance to work with and can't be modified directly from extensions

class GenericViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    // your code here  
    viewController.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeGeneric
  }
}

And use that where ever you need

class viewController: GenericViewController

This is a really good approach since you have control over what implements it and what's not considering that it might not be there in all scenes

Originate answered 5/10, 2020 at 16:42 Comment(0)
G
0

To solve the same problem, I used the swizzling technique

import UIKit

private let swizzling: (UIViewController.Type, Selector, Selector) -> Void = { forClass, originalSelector, swizzledSelector in
    if let originalMethod = class_getInstanceMethod(forClass, originalSelector), let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
        let didAddMethod = class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        if didAddMethod {
            class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UIViewController {
    
    static func swizzle() {
        let originalSelector1 = #selector(viewDidLoad)
        let swizzledSelector1 = #selector(swizzled_viewDidLoad)
        swizzling(UIViewController.self, originalSelector1, swizzledSelector1)
    }
    
    @objc open func swizzled_viewDidLoad() {
        if let _ = navigationController {
            if #available(iOS 14.0, *) {
                navigationItem.backButtonDisplayMode = .generic
            } else {
                // Fallback on earlier versions
                navigationItem.backButtonTitle = "Back"
            }
        }
        swizzled_viewDidLoad()
    }
}

And in application(_:didFinishLaunchingWithOptions:) call

UIViewController.swizzle()
Gazpacho answered 6/11, 2020 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.