5

I have a navigation bar that uses white tint color on a dark background color. I use UIActivityViewController to share a link with the iOS share sheet.

When I select WhatsApp or Messages app to share the content the navigation buttons have the default blue tint color. Also the searchbar (in the case of WhatsApp) has a gray tint color which really makes it hard to read.

I couldn't manage to change the tint color. My code:

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

Any ideas?

Edit:

This is not a duplicate question because it is not about the button tint color of the activity view controller. It is about the button color of the navigation controller of the view controller that is shown after e.g. share on iMessage is pressed.

MJQZ1347
  • 2,607
  • 7
  • 27
  • 49
  • 1
    Possible duplicate of [iOS 8 UIActivityViewController and UIAlertController button text color uses window's tintColor](http://stackoverflow.com/questions/25795065/ios-8-uiactivityviewcontroller-and-uialertcontroller-button-text-color-uses-wind) – Adrian Sep 29 '16 at 13:30
  • 1
    Have you tried using the appearance proxy?`UINavigationBar.appearance().tintColor = UICcolor.whiteColor` – Krumelur Sep 29 '16 at 13:31
  • Yes I did that in the `AppDelegate` but not after presenting activityVC. – MJQZ1347 Sep 29 '16 at 13:33
  • Edit: Just tried it in the completion block without success. – MJQZ1347 Sep 29 '16 at 13:40

3 Answers3

1

Neither of the solutions above have worked for me, but I found yet another way to set the tintColor - via TextAttributes. In your UIActivityViewController in init or viewDidLoad() add the following code (and yes, it is available only since 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)
   }
}

Also, if you aim to change color ONLY for your current ViewController (in our case in UIActivityViewController), don't forget to reset the values on dismiss, since appearance() is a global set of style for your entire app.

roxanneM
  • 853
  • 1
  • 7
  • 12
1

In case of sharing file on iOS 14.2 I had to use all these 3 lines to change color of the "Save", "cancel" und the folder icon:

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

And had to change them back when leaving the view

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

I admit that it looks ugly, but was the only thing that worked for me

brainray
  • 12,512
  • 11
  • 67
  • 116
0

You can set the tintColor for view controllers presented by UIActivityViewController by setting the tintColor on your app delegate's UIWindow instance. Setting tintColor on UIActivityViewController seems to have no effect regardless of when or how you set it.

In your app delegate:

    self.window.tintColor = [theme navBarItemsTintColor];
W Dyson
  • 4,604
  • 4
  • 40
  • 68