改变UIActivityViewController中UIBarButtonItems的色调颜色



我有一个导航栏,在深色背景上使用白色。我使用UIActivityViewController与iOS共享表共享链接。

当我选择WhatsApp或消息应用程序分享内容时,导航按钮具有默认的蓝色色调。此外,搜索栏(在WhatsApp的情况下)有一个灰色的色调,这确实使它很难阅读。

我没办法改变色调。我的代码:

let textToShare = "Visit my website!"
if let myWebsite = NSURL(string: "http://google.com") {
    let objectsToShare = [textToShare, myWebsite]
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    self.presentViewController(activityVC, animated: true, completion: {
        activityVC.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        activityVC.navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.whiteColor()   
        activityVC.navigationController?.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
    })
}

任何想法?

编辑:

这不是一个重复的问题,因为它不是关于活动视图控制器的按钮颜色。它是关于视图控制器的导航控制器的按钮颜色,例如在iMessage上按下share后显示

以上两种解决方案都不适合我,但我发现了另一种方法来设置tintColor -通过TextAttributes。在你的UIActivityViewController在initviewDidLoad()添加以下代码(是的,它是可仅从iOS 11):

override func viewDidLoad() {
   super.viewDidLoad()
   if #available(iOS 11.0, *) {
      let normalAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.purple]
      UIBarButtonItem.appearance().setTitleTextAttributes(normalAttributes, for: .normal)
      let disabledAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.gray]
      UIBarButtonItem.appearance().setTitleTextAttributes(disabledAttributes, for: .disabled)
   }
}

此外,如果你的目标是改变颜色仅为您当前的ViewController(在我们的情况下,在UIActivityViewController),不要忘记重置值在解散,因为appearance()是一个全局设置的风格为您的整个应用程序。

如果在iOS 14.2上共享文件,我必须使用这三行来更改"Save", "cancel"打开文件夹图标:

UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .blue
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .blue
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: .blue], for: .normal)

并且必须在离开视图时将它们更改回来

  activityVC.completionWithItemsHandler = { (_, _: Bool, _: [Any]?, _: Error?) in
  UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
  UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
  UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: .white], for: .normal)
}

我承认它看起来很丑,但这是唯一适合我的东西

你可以通过在你的应用程序委托的UIWindow实例上设置tintColor来为UIActivityViewController呈现的视图控制器设置tintColor。在UIActivityViewController上设置tintColor似乎没有效果,不管你何时或如何设置它。

在你的app委托中:

    self.window.tintColor = [theme navBarItemsTintColor];

最新更新