Search Bar disappeared from view while typing

北慕城南 提交于 2020-01-02 10:14:45

问题


I added SearchBar to my TableView in ViewDidLoad() doing it:

    self.searchBar = UISearchController(searchResultsController: nil)
    self.searchBar.searchResultsUpdater = self

    self.searchBar.dimsBackgroundDuringPresentation = false
    self.searchBar.searchBar.sizeToFit()

    self.tableView.tableHeaderView = self.searchBar.searchBar
    self.tableView.reloadData()

everything works fine, but when I tap on this SearchBar it disappears. It means, I can still typing, and I can see the results but, don't see SearchBar. I implemented UISearchBarDelegate and I have been trying to add

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    self.navigationController?.navigationBarHidden = false
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    self.navigationController?.navigationBarHidden = true
}

but it still doesn't work. Do you have any idea, why this Search Bar disappears?

solution of this problem is (like @sandy sad) write this line of code in viewDidLoad()

 self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true

but now I have a new problem it's mean When I select row in TableView and display new VievController, SearchBar doesn't disappear and I see it in new view. Why?


回答1:


You need to set extendedLayoutIncludesOpaqueBars to true in viewDidLoad().

self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true



回答2:


Actually the search Bar is not hiding it is just adjusting it width and height according to the text.

Remove this line from your code and it will work fine.

self.searchBar.searchBar.sizeToFit()


来源:https://stackoverflow.com/questions/34633176/search-bar-disappeared-from-view-while-typing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!