Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to make SFSafariViewController statusbar style lightContent

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)
    }
}

like image 484
Dhaval H. Nena Avatar asked May 16 '19 06:05

Dhaval H. Nena


1 Answers

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's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen.

Output: Screenshot

enter image description here

like image 169
Rocky Avatar answered Nov 11 '22 22:11

Rocky