0

Is there a way to change UIReferenceLibraryViewController view's "Done" button? Apparently, nesting the view controller inside another NavigationViewController does not really work (see the last screenshot).

The goal is to replace the done button with an SF symbol

let word = "home"
let viewController = UIReferenceLibraryViewController(term: word)
viewController.title = word
viewController.modalPresentationStyle = .fullScreen

present(viewController, animated: true)

enter image description here enter image description here

enter image description here

XY L
  • 25,431
  • 14
  • 84
  • 143

1 Answers1

1

Some hacky approach seems required to achieve what we want to.

  1. Get the target navigation bar view, https://stackoverflow.com/a/54308437/2226315
  2. Add the custom button OR change the title
extension UIView {
    func findViews<T: UIView>(subclassOf: T.Type) -> [T] {
        return recursiveSubviews.compactMap { $0 as? T }
    }

    var recursiveSubviews: [UIView] {
        return subviews + subviews.flatMap { $0.recursiveSubviews }
    }
}
let closeButtonItem = UIBarButtonItem(
    image: UIImage(systemName: "xmark"),
    style: .done,
    target: self,
    action: nil
)
            
let arrowButtonItem = UIBarButtonItem(
    image: UIImage(systemName: "arrow.up"),
    style: .done,
    target: self,
    action: nil
)
            
let word = "home"
let viewController = UIReferenceLibraryViewController(term: word)
viewController.modalPresentationStyle = .fullScreen

let navigationBar = viewController.view.findViews(subclassOf: UINavigationBar.self).first
            
navigationBar!.topItem?.title = word
navigationBar!.items!.first!.setLeftBarButton(closeButtonItem, animated: false)
navigationBar!.items?.first?.rightBarButtonItem? = arrowButtonItem

enter image description here

XY L
  • 25,431
  • 14
  • 84
  • 143