ios - 在 AppDelegate 中设置 UIButton 外观时如何更改 SafariViewController 中导航栏的色调

标签 ios swift uibutton uiappearance sfsafariviewcontroller

当在 AppDelegate 中设置 UIButton.appearance() 时,我试图更改 SFSafariViewController 中导航栏的色调.

例如:

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Here is the global setting that I want to override in the 
       SafariViewController
    UIButton.appearance().tintColor = UIColor.green
}

SFSafariViewController

override func viewDidLoad() {
    super.viewDidLoad()

    // This will change the toolbar tint but not the Done button or the 
       website name in the "navigation" section
    preferredControlTintColor = UIColor.white
}

但是,“完成”按钮和网站名称的颜色仍然是绿色。

我还尝试对应用程序委托(delegate)中的对象进行覆盖。它们都不起作用。

应用程序委托(delegate)(也在 didFinishLaunchingWithOptions 中)

// These are attempts to override the tint set above. None seem to work.
UIButton.appearance(whenContainedInInstancesOf: [SFSafariViewController.self]).tintColor = UIColor.white
UIBarButtonItem.appearance(whenContainedInInstancesOf: [SFSafariViewController.self]).tintColor = UIColor.white
UINavigationBar.appearance(whenContainedInInstancesOf: [SFSafariViewController.self]).tintColor = UIColor.white

我还尝试在 Safari View Controller 本身中设置 View 的色调,但它不会为导航栏着色。

SFSafariViewController(也在viewDidLoad中)

// This can be seen by bringing up an alert like in a peek
view.tintColor = UIColor.purple

这是用 Swift 3.0 编写的,并在 Xcode 8.1 中测试的。

我在 github 上发布了一个示例项目.

任何帮助将不胜感激。谢谢。

(选择颜色是出于测试目的。)

最佳答案

我认为我发现的唯一方法是使用您在 AppDelegate 中编写的相同代码,但在适当的位置。

介绍 safari Controller

//Just before presenting the Safari Controller, change the color to red. It will make the Done button color to red.
UIButton.appearance().tintColor = UIColor.red
let controller = StyledSafariViewController(url: appleUrl)
present(controller, animated: true, completion: nil)

并且在SafariController类的viewWillDisappear方法中

再次将按钮颜色设为全局

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    //resetting your button color
    UIButton.appearance().tintColor = UIColor.green
}

关于ios - 在 AppDelegate 中设置 UIButton 外观时如何更改 SafariViewController 中导航栏的色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323858/

相关文章:

ios - 设置标签字体为粗体和斜体

for 循环中的 Swift 闭包

ios - 我的 segue 退得太远了

iOS/Xcode : Name button created in storyboard name so it can be disabled

objective-c - 在什么情况下,我们需要在 ARC 下编写 __autoreleasing 所有权限定符?

ios - PFQuery 在 Swift 3 转换后未运行

ios - Xamarin.Forms - iOS - 在 UIButton 上放置多个图像的自定义效果

ios - 如何从 UITableViewCell 中的自定义 UIButton 中删除触摸延迟

ios - 如何优化过滤器性能 CIFilter

ios - 如何查找从哪个 View Controller 调用方法