Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController Change Navigation Bar text color

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

like image 615
Simon Avatar asked Jun 02 '17 18:06

Simon


3 Answers

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)
}
like image 52
Hassaan Fayyaz Avatar answered Nov 20 '22 20:11

Hassaan Fayyaz


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

UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .blue
like image 4
crinos Avatar answered Nov 20 '22 19:11

crinos


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)
    }
}
like image 1
ajay_nasa Avatar answered Nov 20 '22 19:11

ajay_nasa