iOS 11,搜索栏从tableHeaderView跳转到顶部

时间:2017-09-28 11:10:58

标签: swift ios11 uisearchcontroller searchbar

我有一个UITableView,我将其标题设置为搜索栏。当我专注于搜索栏时,它会跳到屏幕顶部,在低于iOS11的设备上正常工作,我没有使用带导航栏的视图,我想修复表格顶部的搜索栏

self.definesPresentationContext = true
self.extendedLayoutIncludesOpaqueBars = true;

searchController.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = NSLocalizedString("SEARCH_BOX_TEXT", bundle: bundle!, comment: "Search box text")

searchController.searchBar.sizeToFit()
searchController.searchBar.barTintColor = UIColor.lightGray

searchController.searchBar.setValue(NSLocalizedString("SEARCH_BAR_CANCEL_BUTTON", bundle: bundle!, comment: "Search bar cancel button"), forKey: "_cancelButtonText")

productListTable.tableHeaderView = searchController.searchBar

在:

enter image description here

在我专注于搜索栏之后:

enter image description here

1 个答案:

答案 0 :(得分:0)

我还将搜索栏放入我的tableview标题

所以也许你可以考虑我的答案,它可以在iOS11中正常工作。

就是这样。

在你的故事板中:

enter image description here

代码:

override func viewDidLoad() {

    super.viewDidLoad()

    self.setupSearchController()

}

func setupSearchController() {

    self.searchResultController = UISearchController(searchResultsController: nil)

    self.searchResultController.searchResultsUpdater = self

    self.searchResultController.delegate = self

    self.searchResultController.hidesNavigationBarDuringPresentation = false

    self.searchResultController.dimsBackgroundDuringPresentation = false

    self.searchResultController.searchBar.delegate = self

    self.searchResultController.searchBar.searchBarStyle = .minimal

    self.searchResultController.searchBar.tintColor = UIColor.white

    self.phoneSearchView.searchBarContainer.addSubview(self.searchResultController.searchBar)

    self.memberListTableView.tableHeaderView = self.phoneSearchView

}

override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()

    self.searchResultController.searchBar.sizeToFit()

    self.searchResultController.searchBar.frame.size.width = self.phoneSearchView.searchBarContainer.frame.size.width

    self.searchResultController.searchBar.frame.size.height = self.phoneSearchView.searchBarContainer.frame.size.height

}

结果:

enter image description here