I have a search bar that I have applied a drop shadow to, and as you can see here, the cancel button casts its own shadow too. I would like for the shadow to be limited to the search text box. Here is what I'm starting with:
let searchBar = UISearchBar()
searchBar.placeholder = "KAT Milkshake"
searchBar.backgroundImage = UIImage()
searchBar.barTintColor = UIColor.white
searchBar.compatibleSearchTextField.leftView?.tintColor = UIColor.gray
searchBar.compatibleSearchTextField.backgroundColor = UIColor.white
searchBar.layer.shadowColor = UIColor.black.cgColor
searchBar.layer.shadowOpacity = 0.25
searchBar.layer.shadowOffset = CGSize(width: 2, height: 2)
searchBar.layer.shadowRadius = 5
What I've tried:
I tried altering the cancel button's appearance via
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
, but it turns out that this object's shadow is a separate one. Notice the subtle difference around the letters after I applied another shadow by modifying the appearance shadow properties.Giving the button a white image background didn't work, which I did by accessing
searchBar.value(forKey: "cancelButton") as! UIButton
. The shadow is applied around the whole image and looks even worse.Adjusting the bounds of the search bar layer unfortunately did not work either (
searchBar.layer.frame = CGRect(x: searchBar.frame.minX, y: searchBar.frame.minY, width: searchBar.frame.width - cancelButton.frame.width, height: searchBar.frame.height)
). I gave the search bar layer a border to observe this, and the cancel button shadow persists.I also tried unlinking the cancel button from the superview, but was only able to make it disappear entirely.
Is there any way around this? I'm on Swift 5 and iOS 14.