iOS 11中的UISearchBar对齐问题

时间:2017-11-06 19:05:22

标签: swift ios11 uisearchcontroller

   I have UITableViewController where i was adding UISearchController searchbar as an headerView and I have changed this to navigationItem.searchcontroller for iOS 11 in iOS 11 searchbar won't appear at all even navigationItem was present.

如果不使用此搜索栏,则用户界面将进行折腾。请告诉我这里我做错了什么。

let  searchController = UISearchController(searchResultsController: vc)
            searchController.searchBar.scopeButtonTitles = ["All", "images", "videos", "others"]
            searchController.dimsBackgroundDuringPresentation = false
            // Don't remove these two lines then search bar alignment issue will raise
            self.extendedLayoutIncludesOpaqueBars = true
            self.searchController.searchBar.isHidden = false
            definesPresentationContext = false
            if #available(iOS 11.0, *) {
                 self.navigationItem.searchController = searchController // not working for me.
                searchController?.hidesNavigationBarDuringPresentation = false
                 navigationItem.hidesSearchBarWhenScrolling = false

            } else {
                searchController?.hidesNavigationBarDuringPresentation = true
                searchController?.searchBar.sizeToFit()
                tableView.tableHeaderView = searchController.searchBar
            }

searchbar

问题清单

  • 在iOS 11上,如果将UISearchbar添加到标题视图
  • ,则UI会出现附加图像错误
  • 如果我尝试添加导航项目,如代码中所述,它不会出现
  • 由于我在搜索处于活动状态时使用不同的搜索结果控制器,我需要在当前控制器中禁用操作 - 如何轻松实现此目的。

1 个答案:

答案 0 :(得分:0)

我不确定那里出了什么问题,但在我的情况下,它运作正常。这是我的代码:

  1. 创建了一个searchController

    fileprivate let searchController = UISearchController(searchResultsController:nil)

  2. 在viewDidLoad()中:

    searchController.searchResultsUpdater = self searchController.obscuresBackgroundDuringPresentation = false searchController.searchBar.placeholder = kSearchPlaceholder searchController.searchBar.scopeButtonTitles = [kCategory,kAuthor,kPicture] searchController.searchBar.delegate = self definesPresentationContext = true

    if #available(iOS 11.0,*){   navigationItem.searchController = searchController } else {   tableView.tableHeaderView = searchController.searchBar }

  3. 很高兴。