Not able to see the searchBar in tableView section header everthing is done programmatically no storyboards used
import UIKit
class ConversationsViewController: UITableViewController {
let searchBar:UISearchBar = {
let bar = UISearchBar()
bar.placeholder = "search conversations"
bar.translatesAutoresizingMaskIntoConstraints = false
return bar
}()
let headerView:UIView = {
let hView = UIView()
// hView.backgroundColor = .red
hView.translatesAutoresizingMaskIntoConstraints = false
return hView
}()
// we want custom cell and header view
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
// override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// return "section \(section)"
// }
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerView.addSubview(searchBar)
searchBar.centerXAnchor.constraint(equalTo: headerView.centerXAnchor).isActive = true
searchBar.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
searchBar.widthAnchor.constraint(equalToConstant: 40).isActive = true
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 70
}
}
I tried diff ways but I found this method to add headerView in tableView and now I can't figure out how to add that search bar in headerView , I tried above but it doesn't work , please help thank you

Couple things...
A view for a section header should NOT have
.translatesAutoresizingMaskIntoConstraintsset tofalse. Leave it at the default oftrue.Don't give the
UISearchBara height constraint -- let it use its intrinsic height.We DO need to give the search bar a width constraint. If you want it to fit the width of the table, use:
If you want it to be only partial width, either give that line a negative value for the
constant, or use a multiplier:Here is your class with those modifications: