How can I make an if statement in swift that when I click the search button as shown in the picture below to show the search bar from isHidden
to isSelected
Basically when I press the searchImage
the searchBar
will show from its initial isHidden
to isSelected
@IBAction func searchButton(_ sender: Any) {
}
@IBOutlet weak var searchField: UISearchBar!
CodePudding user response:
I don't know what you need exactly but if you need to switch isHidden or isSelected you can add var to keep the search bar state like:
var isSearchFieldHidden: Bool = false{
didSet{
self.searchField.isHidden = isSearchFieldHidden
}
}
@IBAction func searchButton(_ sender: Any) {
isSearchFieldHidden.toggle()
}
@IBOutlet weak var searchField: UISearchBar!
CodePudding user response:
Try to use
Button(action: {
isHidden.toggle()
}) {
Image("search_icon")
}
!isHidden{
// any view: Button, TextField etc.
your_searchBarView()
}
and read: https://developer.apple.com/documentation/swiftui/text/hidden()#declaration