【问题标题】:How to change tint of navigation bar in SafariViewController when setting UIButton appearance in AppDelegate在 AppDelegate 中设置 UIButton 外观时如何更改 SafariViewController 中导航栏的色调
【发布时间】:2016-10-29 21:29:38
【问题描述】:

UIButton.appearance()AppDelegate 中设置时,我正在尝试更改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
}

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

我还尝试对 App Delegate 中的对象进行覆盖。他们都没有工作。

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 视图控制器本身中设置视图的色调,但它不会为导航栏着色。

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

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

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

【问题讨论】:

  • @Adrian 注意以“SFSafariViewController”开头的部分。
  • @MatthewFrederick 爬回我的洞
  • @Adrian 抱歉,我不是这个意思。

标签: ios swift uibutton uiappearance sfsafariviewcontroller


【解决方案1】:

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

关于展示 Safari 控制器

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

【讨论】:

  • 我清楚地提到,使用这段代码,我只能为这个 safari 控制器更改完成按钮的颜色。在 Xcode 8.1 上测试
  • 酷哥,按照要求它没有用。这就是为什么问?
  • 如果它不起作用,那么 OP 可能不会接受答案 :)
  • 当时我发表评论她不接受。冷静下来,你的代码就像专业人士一样工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多