tim
tim

Reputation: 173

cannot see searchbar when placed at top of UITableViewController

I have placed a searchBar at the top of my UITableViewController and I do not know how to make it visible. I do not want to use searchController.searchBar (which does place the searchbar in the header of the tableView) because I am interested in using the UISearchBarDelegate. I accomplished the above via the storyboard. How do I place a searchbar in the header of the UITableView Controller?

Upvotes: 1

Views: 368

Answers (3)

Hideyasu.T
Hideyasu.T

Reputation: 875

Sometimes SearchBar's height is 0.

I don't know why, but anyway try set constraints height = 44

and in the inspector window, you can set the red corlor for finding it easy.

enter image description here

and inspector window enter image description here

Upvotes: 0

Vikash Kumar
Vikash Kumar

Reputation: 636

Add search bar programmatically in swift 3+

private var searchBar: UISearchBar!

let searchBarView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(45)))
searchBar = UISearchBar()
searchBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: 44)
searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.placeholder = " Search..."
searchBar.sizeToFit()
searchBar.isTranslucent = false
searchBar.delegate = self
searchBarView .addSubview(searchBar)
tableView.tableHeaderView=searchBarView;

Upvotes: 0

V-Dev
V-Dev

Reputation: 508

Just drag n drop search bar at top of table view controller (just above to prototypecell) enter image description here

enter image description here

Upvotes: 3

Related Questions