在AppDelegate

时间:2016-10-29 21:29:39

标签: ios swift uibutton uiappearance sfsafariviewcontroller

我想在SFSafariViewController中设置UIButton.appearance()时更改AppDelegate中导航栏的色调。

例如:

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

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

我还尝试在App 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中设置视图的色调,但它没有为导航栏着色。

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上发布了一个示例项目。

非常感谢任何帮助。谢谢。

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

1 个答案:

答案 0 :(得分:4)

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

展示野生动物园控制器

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