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.
Related
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.
This is probably a simple fix, but I haven't been able to figure out what's going on.
I have a UITableView that looks like this:
When I scroll up, however, the cells are visible behind the title, like so:
I've tried changing the navigation bar to opaque in appDelegate:
UINavigationBar.appearance().isOpaque = true
I have also tried editing the top insets on the UITableView without success. Any thoughts?
If anyone else has the same problem, I just had to set the UINavigationBar tint color like so, and make it non-translucent:
UINavigationBar.appearance().barTintColor = colorDict[currentClerkship]
UINavigationBar.appearance().isTranslucent = false
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)
I have a problem with some borderless NSButtons in a view, on a transparent popover. When the popover is first opened, the buttons look exactly as they should, but when the popover (or the view inside it) gains focus, the background becomes transparent.
The first time it is opened, it looks like this (As it should):
But when the popover gains focus, the buttons end up like this:
, where the background is transparent and the content beneath the popover is visible.
I already tried the following, which I found scattered around the web:
optionsButton.setButtonType(.MomentaryChangeButton)
optionsButton.cell?.showsFirstResponder = false
let bColor = NSColor(red: 230.0/255.0, green: 230.0/255.0, blue: 230.0/255.0, alpha: 1.0)
(optionsButton.cell as! NSButtonCell).backgroundColor = bColor
optionsButton.layer?.backgroundColor = bColor.CGColor
(optionsButton.cell as! NSButtonCell).showsStateBy = .PushInCellMask
(optionsButton.cell as! NSButtonCell).highlightsBy = .ContentsCellMask
optionsButton is, of course, the one on the right. I tried only using some of the code above, but every variation of this doesn't seem to fix it, unfortunately!
Does anyone have any idea on how to avoid this? And does someone know why the trash-button doesn't have the same problem?
Thanks in advance!
The following is tested for NSTextField, but should work for buttons as well.
Set the appearance property of the NSButton to NSAppearanceNameAqua. Because if the button doesn't try to do some weird vibrancy effect, he can't mess things up. The labels still look the same and the strange effect is gone.
My words in code:
self.button.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
Updated: SWIFT 4
The following is tested for NSButton:
NameofButton.appearance = NSAppearance(named: NSAppearance.Name.aqua)
I've managed to change the navBar tint, the background color and labels' color using this.
But is it possible to change the icons' color?
(those left to tableview's labels)
Thanks!
I don't know of a way to change the icon colors, but I don't really think all this hacking is necessary anyway - Apple certainly doesn't recommend it.
As far as I can tell, there is nothing special about the moreViewController, apart from being hard to customize. If it were me, I'd simply create my own custom viewcontroller, say MoreViewController, as a subclass of UITableViewController, add it to a NavigationController and then add that as the fifth and last item in the TabBarController. This table would then have a cell for each additional viewcontroller that I'd like to show. Then I'd be free to customize these cells to my heart's content.
This is an old question, nevertheless I'll post a fairly reasonable solution I found.
I first tried setting the global tint color to what I wanted, but that didn't work. Luckily, simply changing the tint color of the table view did work. Adapting code from here:
let color: UIColor
// ...
if let moreTableView = moreNavigationController.topViewController?.view as? UITableView {
moreTableView.tintColor = color
}
This still doesn't seem to affect the edit view controller, however.
EDIT:
There is actually a simpler way to do both, see this answer.