Some hacky approach seems required to achieve what we want to.
- Get the target navigation bar view, https://stackoverflow.com/a/54308437/2226315
- 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
