如何更改 SFSafariViewController 工具栏颜色

How to change SFSafariViewController ToolBar color

预期输出: 我想将工具栏颜色更改为深黑色。

实际输出: 工具栏为浅灰色。

代码如下:

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)

iOS 10 API

的更新答案

SFSafariViewController 现在有 preferredBarTintColorpreferredControlTintColor 属性来控制工具栏的外观。


原答案

SFSafariViewController 渲染进程外。您只能更改色调颜色,但不能更改条形样式或条形色调颜色。

要设置色调,请像这样设置 Safari 控制器视图的色调:

let sfController = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
sfController.view.tintColor = UIColor.redColor()
navigationController?.showViewController(sfController, sender: self)

我看不出有什么办法可以改变工具栏的背景颜色,但是可以改变工具栏中按钮的颜色。

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

如我所见,所有其他外观更改或直接在控制器属性中更改均无效。

有两种方法:

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

并且:

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

祝大家好运!

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