13

Expected Output: I want to change the ToolBar color to Dark Black.

Actual Output: ToolBar is light Grey color.

Here is the code:

let webViewController = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
self.navigationController?.toolbar.barTintColor = UIColor.blackColor()
self.navigationController?.toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.toolbar.barStyle = UIBarStyle.Black
self.navigationController?.pushViewController(webViewController, animated: true)
Srikanth Adavalli
  • 665
  • 1
  • 10
  • 25

4 Answers4

27

Updated Answer for iOS 10 API

SFSafariViewController now has preferredBarTintColor and preferredControlTintColor properties to control how the toolbars look.


Original Answer

SFSafariViewController renders off-process. You can only change the tint color, but not bar style or bar tint color.

To set the tint color, set the Safari controller's view's tint color like so:

let sfController = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
sfController.view.tintColor = UIColor.redColor()
navigationController?.showViewController(sfController, sender: self)
shim
  • 9,289
  • 12
  • 69
  • 108
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
5

There are two ways:

let resetPasswordSafari = SFSafariViewController(url: url, entersReaderIfAvailable: true)
resetPasswordSafari.preferredBarTintColor = .mainColor
resetPasswordSafari.preferredControlTintColor = .black

And:

class ResetPasswordSafariViewController: SFSafariViewController {

  override init(url URL: URL, entersReaderIfAvailable: Bool) {
    super.init(url: URL, entersReaderIfAvailable: entersReaderIfAvailable)
    delegate = self

    preferredBarTintColor = .blue
    preferredControlTintColor = .black
  }
}

// MARK: - SFSafariViewControllerDelegate

extension ResetPasswordSafariViewController: SFSafariViewControllerDelegate {
  internal func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
    controller.dismiss(animated: true)
  }
}

Good luck all!

Mihail Salari
  • 1,471
  • 16
  • 17
  • what's `internal func safariViewControllerDidFinish` for? and will apple approve apps that subclass `SFSafariViewController`? – spnkr Sep 04 '19 at 20:10
  • @spnkr yes, will approve. Here's the definition of "internal" access level: Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure. – Mihail Salari Sep 05 '19 at 08:38
0

I see no way to change background color of ToolBar, but it is possible to change color of buttons in ToolBar.

[UIBarButtonItem appearance].tintColor = [UIColor whiteColor];

All other changes in appearance, or directly in controller properties, have no effect, as I see.

nominanza
  • 201
  • 2
  • 6
0

//To make changes in SFSafariViewController

     if let url = URL(string:"https://sandydhumale.business.site") {
        let config = SFSafariViewController.Configuration()
        config.entersReaderIfAvailable = true
        config.barCollapsingEnabled = true
        let vc = SFSafariViewController(url: url, configuration: config)
        vc.dismissButtonStyle = .close
        vc.preferredBarTintColor = .green // Your choice color
        vc.preferredControlTintColor = .white // All buttons/items color
        self.present(vc, animated: true, completion: nil)
    }
tripleee
  • 175,061
  • 34
  • 275
  • 318