0

I'm trying to figure out how to add a searchbar and a scope as a header to my UIViewController programmatically I want it to be at the top of the safe area. The issue is I don't know the code to add it at the top of the safe area below the title of the UIViewController and bar button item.

func customizeSearch() {
    let searchBar = UISearchBar(frame: CGRect(x:0, y:0, width: (UIScreen.main.bounds.width), height: 70))
    searchBar.backgroundColor = UIColor.black
    searchBar.delegate = self
    searchBar.scopeButtonTitles = ["All", "Forwards", "Defensemen", "Goalies"]
    /*self.tableView.tableHeaderView = searchBar if I had a tableview, I have a vc instead.*/
  }
BarO
  • 13
  • 2

1 Answers1

1

You perhaps want to add your search bar to the navigation bar of the view controller (like the header of the VC). If that's the case, you can assign it to titleView of your Navigation Item:

self.navigationItem.titleView = searchBar

Check this link if you want more details: Search bar in navigation of vc.

aheze
  • 24,434
  • 8
  • 68
  • 125
  • Alright the searchbar shows up but I am wondering how I can have it t below the title at the top of the safe area ,I should of specified that more. Also my scopedbar seems to not be added as well. – BarO Apr 12 '21 at 03:25