Programmatically UISearchBar not showing up

Viewed 584

I created a programatically UISearchBar (not UISearchController) and I set it as tableview header. However, it's not showing up. Anyone might know why is that?

Outside of the class:

var searchBar = UISearchBar()

Inside the viewDidLoad method.

    searchBar.barTintColor = UIColor(red:0.54, green:0.77, blue:0.80, alpha:1.0)
    searchBar.placeholder = "Cauta cheltuieli"
    searchBar.backgroundImage = #imageLiteral(resourceName: "searchbarback")

    tableView.tableHeaderView = searchBar
    tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: true)
1 Answers

You forget to give the searchbar any dimension so the size will be zero.

Try setting the dimension by setting the frame or use the constructor with a frame.

let searchbar = UISearchBar(frame: CGRect(x: 0.0, y: 0.0, height: 50.0, width: tableView.frame.width))
Related