0
votes

I'm testing a simple animation to hide a uiSearchBar with a button tap.

var searchBarActive = false
override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Search", style: .done, target: self, action: #selector(searchTapped))
}

@objc func searchTapped(sender: UIBarButtonItem) {
    if searchBarActive {
        searchBarActive = false
        UIView.animate(withDuration: 2) {
            self.searchBar.isHidden = true
//            self.searchBar.searchBarStyle = .default
//            self.searchBar.barStyle = .default
//            self.searchBar.tintColor = .none
//            self.searchBar.barTintColor = .none
//            self.searchBar.tintAdjustmentMode = .normal
//            self.searchBar.isTranslucent = false
        }
    } else {
        searchBarActive = true
        UIView.animate(withDuration: 2) {
            self.searchBar.isHidden = false
        }
    }
}

The animation works fine, but I noticed when isHidden is set to true, the searchBar seems to turn translucent, because the color it gets is the color of the view that is behind it. I noticed that because I was using a random color for that view, if you use white for example, you wouldn't even notice.

I undertand that it would appear to be changing its color if the isHidden property would fade the bar, but by setting the animation with a high duration it looks like it comes from the top to the bottom (like a sliding motion) when isHidden is set to false and from bottom to top when isHidden is set to true.

I tried the commented code (both inside and outside the animation closure) expecting it would fix it, but it looks like something is still overriding these values. I don't have anything else that would cause this problem in the project. I checked this question as well: Setting translucent to NO on UISearchBar and then tried the isTranslucent one, but still didn't work.

Is this expected or should I be doing something different?

Thank you in advance!

1

1 Answers

0
votes

As I have not find an answer to the question, I did resolve it by making the background color that is in the view behind this search bar white. I have this search bar inside a stack view, so I just changed the stack view background color to white.

It fixes the problem but still I wonder why that happens.