I have a requirement of light content in status bar with black background, however some of the screen needs black status bar content with white background, hence I've kept View controller-based status bar appearance to YES in info.plist to adopt status bar style based on view controllers requirement.
My problem is whenever I present SFSafariViewController from any view controller it is taking black status bar content and white background by default i.e status bar style is .default everytime.
I tried overriding preferredStatusBarStyle in SFSafariViewController subclass and no look so far.
Below is my code
import UIKit
import SafariServices
extension SFSafariViewController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .lightContent
}
}
class MyViewController: UIViewController, SFSafariViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.lightGray
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
@IBAction func presentSafari(sender: AnyObject) {
let safari = SFSafariViewController(url: URL(string: "https://www.google.com/")!)
safari.delegate = self
present(safari, animated: true) {
}
}
// MARK: - SFSafariViewControllerDelegate
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
dismiss(animated: true, completion: nil)
}
}
Set modalPresentationCapturesStatusBarAppearance
to takes over control of status bar appearance from the presenting view controller.
@IBAction func presentSafari(sender: AnyObject) {
let safari = SFSafariViewController(url: URL(string: "https://www.google.com/")!)
safari.delegate = self
safari.modalPresentationCapturesStatusBarAppearance = true
if #available(iOS 10.0, *) {
safari.preferredBarTintColor = .yellow
} else {
// Fallback on earlier versions
safari.view.tintColor = .yellow
}
present(safari, animated: true) {
}
}
extension SFSafariViewController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
When you present a view controller by calling the
present(_:animated:completion:)
method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller'smodalPresentationStyle
value isUIModalPresentationStyle.fullScreen
. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen.
Output: Screenshot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With