8

I am creating a UIActivityViewController and trying to change its text color after tapping the share messages icon.

By default, I set my navigation bar text colors to white in the AppDelegate like so

UINavigationBar.appearance().tintColor = whiteColor
UIBarButtonItem.appearance().tintColor = whiteColor

However for just the UIActivityViewController I want to make it the default (i.e. black title text, and the blue Cancel button)

I have tried the following to no luck:

let shareText = "text to share"
let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: [])

activityViewController.navigationController?.navigationBar.tintColor = UIColor.black
activityViewController.navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.blue

present(activityViewController, animated: true, completion: nil)

The result is still the same with white text:

If you look closely in the image, the navigation bar has white text in the title and right bar button items.

enter image description here

5
  • 1
    use UINavigationBar.appearance().tintColor = black while present and UINavigationBar.appearance().tintColor = whiteColor while dismiss. Commented Jun 2, 2017 at 18:56
  • How would I determine if it is present or not? Commented Jun 5, 2017 at 6:09
  • what you need to determine color ? Commented Jun 5, 2017 at 6:11
  • Is it on iOS 11? @KKRocks's comment does not work in iOS 11 Commented Oct 31, 2017 at 20:15
  • @Simon Did you found a solution for this issue? Commented Jul 5, 2019 at 2:29

5 Answers 5

10

For someone else trying to do something similar to this. UIActivityViewController gives us a callback on its completion. What we can do is, change UIBarButtonItem color and then, on its completion, set it back to our original color.

For me, the completion handler is not correctly setting the color back to original, so i am gonna have to do it in the viewWillAppear.

// Setting the required color for the bar buttons on share activities
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.yourRequiredColor], for: .normal)

// Re-setting the default color for the bar buttons again on activity's completion
activityViewController.completionWithItemsHandler = { (activityType, completed:Bool, returnedItems:[Any]?, error: Error?) in
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.yourOriginalColor], for: .normal)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would add that you should also set UIButton.appearance() - certain share actions (for example the Save to files create folder button) are not affected by UIBarButtonItem.
4

In iOS 11 you can change cancel button color in "share via message screen" by setting:

UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .blue

Comments

1

I have tried all above code but it was breaking while cancel and open action sheet again. So I come up with below solution, hope it works for other too

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

Comments

0

If you customize your navigation bar at app delegate class, you should change it just before share actions (when share button is pressed, etc.) but do not forget to change it back when share action is completed or dismissed so you need to customize your navigation bar at viewWillAppear func at the class which you have share action.

It should be like that:

class ShareActionViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //this should be same with your appDel settings:
        UINavigationBar.appearance().tintColor = .white
        UINavigationBar.appearance().barTintColor = .appPurpleColor
    }

    fileprivate func shareButtonPressed() {
        UINavigationBar.appearance().tintColor = .purple
        UINavigationBar.appearance().barTintColor = .white 
    }

}

Comments

-1

I'm using iOS 12 and just worked when I did this:

static func setSystemNavigationBar() {
    UINavigationBar.appearance().tintColor = .black
    UINavigationBar.appearance().titleTextAttributes =
        [NSAttributedString.Key.foregroundColor : UIColor.black,
         NSAttributedString.Key.font: UIFont(name: "Aller", size: 20)!]
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.blue], for: .normal)
}

You can change the font and color as you wish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.