I need to change the color of a disabled toolbar button. I have tried multiple methods.
1.
button.isEnabled = false
button.tintColor = UIColor.blue
2.
button.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .disabled)
Neither of them seems to work. Is there a way to change the color of a disabled toolbar item?
note: this item was created in the storyboard and I am loading it from an outlet
edit: I should have explained this earlier but the buttons have pictures and the tint color seems to be the only thing that works for changing the color of the image.
It's Xcode bug. Use UIButton instead. You can just drag and drop it into the tabbar in storyboard.
And then just use title color:
button.setTitleColor(.blue, for: .disabled)
Swift 5
I didn't want my image to be lighter so what worked for me was to force to use that same image for disabled status, like this:
button.setBackgroundImage(UIImage(named: "MyBackgroundColor"), for: .disabled)
Related
When using the new .searchable() modifier in SwiftUI on iOS 15 I have no way to customize the Search Bar appearance. Specifically, I wan't it to look good with the color I'm using for my Navigation Bar.
I tried altering the .appearance() like this.
UISearchBar.appearance().backgroundColor = UIColor.white
UISearchBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().barTintColor = UIColor.white
But only managed to get this.
Even though this kind of succeeded the spacing does not look good. I would rather tint it white.
I solved this by using these two global appearance lines.
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .white
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
Setting the .font and .foregroundColor modifiers after .searchable will do the desired result, but it still doesn't work with .background modifier as the time of this writing (iOS 16).
The textColor of the input field can be changed from black to white by altering the .preferredColorScheme() of the view to which the .searchable modifier is applied. Setting .preferredColorScheme(.dark) will set the color to white.
in iOS 13 apps, I used the following snippet (placed it in SceneDelegate) to set a custom back image for navigation views (SwiftUI views) which worked perfectly:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "myBackImg")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
UINavigationBar.appearance().tintColor = .gray
/// removing back text from back button (will affect also the text in any button in the nav bar if exists)
UIBarButtonItem
.appearance(whenContainedInInstancesOf: [UINavigationController.self])
.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem
.appearance(whenContainedInInstancesOf: [UINavigationController.self])
.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .highlighted)
but unfortunately, this doesn't work with iOS 14 devices using Xcode 12.0.1 which doesn't show the custom back image.
is there any way to set a custom back image for the back button item for all SwiftUI views in the whole app at once on iOS 14 ?
I have a strange visual bug in my app that only applies to iOS 13 running from an Xcode 11 build. I have a table view embedded in a Navigation Controller with the default tint color set to my app's primary orange color. On iOS12, when you cancel the search action, you are presented with a back button that follows the global nav controller tint of primary orange. This is the expected behavior. Image shown below:
However, this same code in iOS13 produces a system default BLUE back arrow, as shown below:
I have tried EVERYTHING to try and override that blue back button, including creating a custom Bar Button Item with a custom action, but that is way too messy and I want to just simply override the tint color. I've tried the obvious searchController.searchBar.tintColor = UIColor(named:"Primary") where searchController is my UISearchController, and I have tried to override the self.navigationController tint color. I've tried accessing the SearchBar natively, like this: UISearchBar.appearance().tintColor = UIColor(named:"Primary"), but still no luck. I've tried everything else I can think of in the IB, but I can not figure out how to reach this back button's tint color. Can anybody help?
The only way I found so far to get this fix on iOS13.1 is to iterate through the subviews in the navigation bar and manually modified the tintColor.
None of the new UINavigationBarAppearance methods looks like they fix the problem. If you modified the backButtonAppearance in UINavigationBarAppearance I have been able to fix the title in back button but I haven't found a way to fix the image (<).
try this
override func viewWillAppear(_ animated: Bool) {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem?.tintColor = .blue
}
In iOS 13 there are few new appearance types for the navigation bar. When you want to customize navigation bar which associated with a large title or any kind of scroll view just setup appearance parameters for .scrollEdgeAppearance
if #available(iOS 13.0, *) {
let standartAppearence = UINavigationBarAppearance()
standartAppearence.configureWithDefaultBackground()
// Your configuration
UINavigationBar.appearance().scrollEdgeAppearance = standartAppearence
}
It turns out that this was an XCode/Swift bug only affecting iOS 13.1. It should not be something that you have to account for in code, since only a very small portion of the user base is still on 13.1.
I have been trying to solve a problem, but cannot seem to find a solution.
I have a tableviewcell which contains a button. the button has an image set to it. I would like to change the image when the button is pressed, but I am lacking the proper code for this and where the code needs to be inserted (either in the code inside of tableviewcontoller or tableviewcell file).
I know that the normal code is: self.practicePlayButton.setImage(UIImage(named: "play4.png"), forState: .Highlighted)
for changing the buttons... But I seem to be stumped here.
self.practicePlayButton.setImage(UIImage(named: "play4.png"), forState: .Highlighted)
Replace this...
self.practicePlayButton.setImage(UIImage(named: "play4.png"), forState: .Selected)
I notice something strange happens to one of my view controller: the back button disappears, yet it's possible to go back to previous view controller by tapping the top left corner (i.e where the button should reside).
In my entire file there's no line that set self.navigationItem.hidesBackButton to YES; also NSLog prints 0 as self.navigationItem.hidesBackButton's value in viewDidLoad.
This occurs in both the simulator and real device. Any ideas?
Oh dear. In the implementation of the previous view controller, I accidentally set self.title to #"", which causes this annoying bug.
Remove this line solves the problem.
I had a recursive navigation controller, and this also happened to me, I used this code to fix it:
self.navigationItem.leftItemsSupplementBackButton = true
Just in case someone is facing this issue with a custom back button and the above fixes did not work, here is a similar issue I faced with a different solution.
I had a customized back button with text that was disappearing while the arrow could be seen UINavigationController custom back button disappears from NavigationBar
So if anyone is facing a similar situation with disappearing back button text on a customized back button, here is my scenario and fix.
I customized my back button inside a custom NavigationController class as follows:
private func customizeBackButton() {
let backImage = UIImage(named: "BackButton")?.withRenderingMode(.alwaysOriginal)
navigationBar.backIndicatorImage = backImage
navigationBar.backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .normal)
}
This gave me:
Now when I tapped on the back button text, the text disappeared:
I made sure that I followed all the above answers such as setting titles making sure the tint color is valid etc. however this did not work.
In my case, I needed to set attributes even for the highlighted state of the back button as follows:
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .highlighted)
After this, the back button text never disappeared