UIActivityViewController: access the navigation items to change color of presented view - swift

I am changing a project colors and this old orange color not changed on UIActivityViewController button. I want to change button color orange to white.
This look is when user press the share button and select "Add to Notes" button. How can I access these buttons and change colors?
First problem solved. This code is working on buttons:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
When I click to "New Note" this look is came. How can I change the title color on this view?
I want to change this title color black to white.
This is my sharing code and I was trying every bar tint color code.
func shareLink(_ link: String) {
let activityViewController = UIActivityViewController(activityItems: [link], applicationActivities: nil)
activityViewController.navigationController?.navigationBar.barTintColor = .white
activityViewController.navigationController?.navigationBar.tintColor = .white
activityViewController.tabBarController?.navigationItem.backBarButtonItem?.tintColor = .white
activityViewController.tabBarController?.navigationController?.navigationBar.tintColor = .white
self.present(activityViewController, animated: true)
}
Also I tried adding some code on AppDelegate in didFinishLaunchingWithOptions function
UINavigationBar.appearance().tintColor = .white
UIApplication.shared.keyWindow?.tintColor = .white

Ok. These buttons are not accessible. So I solved the problem as follows:
It's for all UINavigationBar buttons:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
It's for all UINavigationBar titles:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

Related

Xcode 13 - Navigation bar and status bar text color changing in swift OS 15

Recently I updated my Xcode to 13 and after that, I am facing some issues with the navigation bar and Status bar. I am using the tab bar in my view controller. After updating the Xcode, according to the version, I added some code related to the navigation bar.
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
Everything is working when I first opened the app. When I click on the other tab and then this tab. The status bar text color is changing.
I tried different ways to set the status bar text color. But nothing worked for me.
Use this function:
extension YourViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
}
If you just want to set to white colour you status bar in the entire project you can easily do it by adding the following keys into your project.plist file:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
So, what does these keys do... You are just defining that you status bar colour will not be controlled by the any view controller and that the default colour will be "light theme" that is pretty much the white colour that you want in this case...
this would be the best approach if you do not intent to update the status bar colour all over the place.
this was tested and is working as expected on Xcode 13 and iOS [13.x,14.x,15.x]
If you want to update to customise or update the colours for the navigation bar you would should be able to do with an extension like this one bellow, just few examples but you guys should get what I mean:
extension UINavigationBar {
func clearBackground() {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navBarAppearance.shadowColor = UIColor.clear
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
func blackColor() {
// Create an Appearance and customise as you wish.
// in this case just a black background with titles in white.
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = UIColor.black
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
}
with this code you should be able to easy update on any view controller by just calling something like:
override func viewDidLoad() {
super.viewDidLoad()
// set the navigation bar color as transparent
self.navigationController?.navigationBar.clearBackground()
}

How can I change the font and color of the Navigation Bar back button

I am trying to change the font and color of the Navigation Bar button.
Ideally, I'd like to change it to black and use a custom font that I already have set up. Is there any way to modify the existing button?
I tried this in my view's init for the font
UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
But I think this only changes the title of an inline Nav bar
It's a UIBarButtonItem so set this property.
init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}
Or you can use this also,
init() {
let standard = UINavigationBarAppearance()
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}
You can do the method above or:
Go to your Assets -> Set a colour for Accent, although this will affect other components in your app i.e. DisclosureGroup button etc.

How to change the background color of navigationBar

I am trying to change the background color of the navigation bar to black but without success. I have checked the answer to this related question but none of the solutions have worked. This is what I have tried:
navigationController?.navigationBar.backgroundColor = UIColor.black
UINavigationBar.appearance().backgroundColor = UIColor.black
You can also change the navigation bar color for a specific view controller, this way:
extension UIViewController {
func setCustomNavigationColor(color: UIColor = .black, isTranslucent: Bool = false ){
self.navigationController?.navigationBar.barTintColor = color
self.navigationController?.navigationBar.isTranslucent = isTranslucent
}
}
call this from viewDidLoad()
setCustomNavigationColor()
Try this:
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().isTranslucent = false
Note: This changes the bar colour for the entire app.

White line under navigation bar when scrolling or until pressing on the search bar

As you can see in the video, there is a white thin line under the navigation bar the moment i start scrolling.
It would only disappear when I would press on the searchBar (contained by my searchController, so the search bar is not added from the Storyboard). I tried a lot of different combinations in order to try to make it disappear but nothing worked.
Any help is appreciated! Thanks!
Video: https://www.youtube.com/watch?v=KcgZmBg1VS0
This is the code inside my viewDidLoad:
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = "Search for a recipe"
searchController.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
searchController.searchBar.tintColor = UIColor.white
Try to set background color and change shadow of the navigationBar. It possibly connected with navigationBar.
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
Try to add this line:
searchController.searchBar.backgroundColor = navigationController?.navigationBar.barTintColor
In the file setupSearchBar()
After code fix:
You can remove that shadow below your navigation bar by the way. I have created extension for doing this:
extension UINavigationBar {
func shouldRemoveShadow(_ value: Bool) -> Void {
self.setValue(value, forKey: "hidesShadow")
}
}

Change UIBarButtonItem from UISearchBar

I'd like to change the text font and color of the Cancel button inside the UISearchBar in iOS 8. I've tried the solutions for iOS 6 and 7 already and they don't seem to work.
This was not the solution I was looking for, but it worked.
let cancelButtonAttributes: NSDictionary = [NSFontAttributeName: FONT_REGULAR_16!, NSForegroundColorAttributeName: COLOR_BLUE]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, forState: UIControlState.Normal)
What you are looking for is:
//The tintColor below will change the colour of the cancel button
searchBar.tintColor = UIColor.blueColor()
But these are also useful While we are on the subject:
searchBar.placeholder = "Placeholder"
//The barTintColor changes the colour of the flashing text indicator
searchBar.barTintColor = UIColor.redColor()
//This sets the cancel button to show up
searchBar.setShowsCancelButton(true, animated: true)
//This makes the searchBar become active
searchBar.becomeFirstResponder()