Change the color of the UIActivityViewController navigation bar

I am creating a UIActivityViewController and trying to change its text color after clicking on the sharing message icon.

By default, I set the colors of the text in the navigation bar to white AppDelegateas follows.

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

However, only for the UIActivityViewController I want to set it by default (ie. Black title and blue button " Cancel).

I tried the following to get out of 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 carefully look at the image, the navigation bar will have white text in the title bar and buttons of the right panel.

enter image description here

+6
source share
4 answers

iOS 11 "share via screen", :

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

- - . UIActivityViewController . , UIBarButtonItem, , , .

, 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 completion
activityViewController.completionWithItemsHandler = { (activityType, completed:Bool, returnedItems:[Any]?, error: Error?) in
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.yourOriginalColor], for: .normal)
}
+2

, ( ..), , , viewWillAppear func , .

:

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 
    }

}
0

iOS 12 , :

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

.

-1

Source: https://habr.com/ru/post/1678440/


All Articles