I have a UISearchBar in my UIStackView with a button, making a sort of nav bar. Here is what it normally looks like:
but when it becomes first responder the UISearchBar's width goes to 0, and it looks like this:
Now the only constraint I have is the width of the UIButton on the right is 44pt and the whole stack view is pinned to the top and sides of the screen. Here's what the console printed out:
I'm not very fluent in NSLayoutConstraint
but clearly something is conflicting with the 44pt width constraint on the button. Why is the UISearchbar's width becoming 0?
Here is my code:
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
if presentationStyle == .expanded {
searchController.searchBar.becomeFirstResponder()
searchController.searchBar.tintColor = UIColor.white
}
}
func didDismissSearchController(_ searchController: UISearchController) {
requestPresentationStyle(.compact)
}
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
}
func configureSearchController() {
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = false
searchController.searchBar.delegate = self
searchController.delegate = self
searchController.searchBar.barTintColor = addButton.backgroundColor
stackView.insertArrangedSubview(searchController.searchBar, at: 0)
//This removes the hairline
let color = addButton.backgroundColor
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = color?.cgColor
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
if presentationStyle == .expanded {
return true
} else {
requestPresentationStyle(.expanded)
return false
}
}