QLPreviewViewController
can have more than 1 toolbar inside. That's why you need to find all UIToolbar
in subviews and hide them.
Also you need observe change of hidden
property because when user tap in QLPreviewViewController
it change visibility of toolbars and navigation bars.
Swift 3:
var toolbars: [UIToolbar] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
toolbars = findToolbarsInSubviews(forView: view)
for toolbar in toolbars {
toolbar.isHidden = true
toolbar.addObserver(self, forKeyPath: "hidden", options: .new, context: nil)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
for toolbar in toolbars {
toolbar.removeObserver(self, forKeyPath: "hidden")
}
}
private func findToolbarsInSubviews(forView view: UIView) -> [UIToolbar] {
var toolbars: [UIToolbar] = []
for subview in view.subviews {
if subview is UIToolbar {
toolbars.append(subview as! UIToolbar)
}
toolbars.append(contentsOf: findToolbarsInSubviews(forView: subview))
}
return toolbars
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let keyPath = keyPath,
let toolbar = object as? UIToolbar,
let value = change?[.newKey] as? Bool,
keyPath == "hidden" && value == false {
toolbar.isHidden = true
}
}