How to change SFSafariViewController ToolBar color
Asked Answered
P

4

14

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)
Palimpsest answered 11/2, 2016 at 7:4 Comment(4)
So what happens. Is your code not working or what ? Please add more details.Bootjack
@Mack Tool Bar Code is not taking impact on SFSafariViewController Bottom.Palimpsest
you try to set tintcolor ?Aminta
@BlackbirdSR-71 self.navigationController?.toolbar.tintColor = UIColor.whiteColor() i already did. but it is not taking any impactPalimpsest
N
28

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)
Numbers answered 12/2, 2016 at 15:27 Comment(5)
i was able to change the color of navigation bar. But my client req is to change the color of toolbar.Palimpsest
@Audi It's not possible. Open an enhancement request with Apple.Sufferance
or you can use wkwebview and make your own toolbarBasra
Make sure that you're not setting the tint for the window or for all views using appearance somewhere else or this won't work.Punchinello
Tried the preferred properties and didn't work. Tried removing all calls to appearance and still does not work.Aceydeucy
B
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!

Bertrando answered 14/1, 2019 at 10:55 Comment(2)
what's internal func safariViewControllerDidFinish for? and will apple approve apps that subclass SFSafariViewController?Karttikeya
@Karttikeya 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.Bertrando
P
1

//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)
    }
Plio answered 26/5, 2021 at 7:20 Comment(0)
V
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.

Vereeniging answered 13/8, 2016 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.