如何更改SFSafariViewController工具栏颜色

13

期望输出: 我想将工具栏颜色改成深黑色。

实际输出: 工具栏的颜色是浅灰色。

以下是代码:

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)

1
那么发生了什么?你的代码不工作了还是怎么样?请添加更多细节。 - Mayur Prajapati
@ Mack 工具栏代码对 SFSafariViewController 底部没有影响。 - Srikanth Adavalli
你尝试设置tintcolor吗? - Raju
@BlackbirdSR-71 self.navigationController?.toolbar.tintColor = UIColor.whiteColor() 我已经这样做了,但没有任何影响。 - Srikanth Adavalli
4个回答

28

iOS 10 API的更新解答

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


原始解答

SFSafariViewController运行在进程外。你只能更改色调颜色,但无法更改栏样式或栏色调颜色。

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

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

我已经成功地更改了导航栏的颜色。但是我的客户要求更改工具栏的颜色。 - Srikanth Adavalli
1
@Audi 不可能的。请向苹果提出增强请求。 - Léo Natan
你可以使用WKWebView并创建自己的工具栏。 - hariszaman
1
确保您没有在其他地方使用appearance来设置窗口或所有视图的色调,否则这将无效。 - Rivera
尝试了首选属性,但没有起作用。尝试删除所有对appearance的调用,仍然不起作用。 - Eric Alford

5
有两种方式:
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)
  }
}

祝大家好运!


internal func safariViewControllerDidFinish 是用来做什么的?苹果会批准那些子类化 SFSafariViewController 的应用吗? - spnkr
@spnkr 是的,会批准。以下是“internal”访问级别的定义:内部访问使实体可以在其定义模块中的任何源文件中使用,但不能在该模块之外的任何源文件中使用。通常在定义应用程序或框架的内部结构时使用内部访问。 - Mihail Salari

0

我看不到改变工具栏背景颜色的方法,但是可以改变工具栏按钮的颜色。

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

所有外观上的更改,或直接在控制器属性中进行的更改,都没有任何影响,我认为。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接