NSButton background transparent after getting focus - swift

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)

Related

Customize "searchable" Search field SwiftUI iOS 15

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.

iOS 13 SearchController Back Button Color

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.

UITableViewCells visible behind title when scrolling up in swift

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

Removing gray gradiant from UITabBar

This is the first time I'm using a UITabBar and I'm having problems getting it looking the way I want to look.
Right now it still has some grey shadow over cast over it. I cannot seem to remove this!
I've added this line to my code but the shadow still keeps showing up:
self.universalTabBar.setValue(true, forKey: "_hidesShadow");
Can anyone tell me what I'm doing wrong?
Try setting the rendering mode of each tab bar item's image.
universalTabBar.tintColor = UIColor.whiteColor()
for tabBarItem in universalTabBar.items! {
tabBarItem.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}

UINavigationController's back button disappears?

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