如何更改SFSafariViewController ToolBar颜色

时间:2016-02-11 07:04:24

标签: objective-c iphone swift uitoolbar sfsafariviewcontroller

预期产出: 我想将ToolBar颜色更改为Dark Black。

实际输出: ToolBar是浅灰色。

以下是代码:

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)

4 个答案:

答案 0 :(得分:22)

更新了iOS 10 API的答案

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

原始答案

SFSafariViewController呈现进程。您只能更改色调颜色,但不能更改条形样式或条纹色调。

要设置色调颜色,请设置Safari控制器的色调颜色,如下所示:

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

答案 1 :(得分:0)

我认为无法改变ToolBar的背景颜色,但可以在ToolBar中更改按钮的颜色。

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

正如我所见,所有其他外观变化或直接在控制器属性中都没有任何影响。

答案 2 :(得分:0)

有两种方法:

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

祝你好运!

答案 3 :(得分:0)

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