当我单击搜索栏上的“取消”时,它消失了

时间:2020-09-05 19:49:20

标签: ios swift

我正在使用UISearchBar,当我单击搜索栏上的“取消”按钮时,它消失了。将UITableView移动到搜索栏的顶部,搜索栏下方消失。这是我的代码:

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
    searchController.searchBar.isTranslucent = true
    searchController.hidesNavigationBarDuringPresentation = false
    
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    
    
    definesPresentationContext = true
    tableView.separatorStyle = .none

    let searchBar = searchController.searchBar
    searchBar.backgroundColor = UIColor.white
    searchBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: 44)
    searchBar.searchBarStyle = UISearchBar.Style.prominent
    searchBar.placeholder = " Search book title, author..."
    searchBar.sizeToFit()
    view.backgroundColor = UIColor.white
    searchBar.isTranslucent = true
    searchBarView.addSubview(searchBar)
    innerView.addSubview(searchBarView)

1 个答案:

答案 0 :(得分:0)

这可能是因为在单击“取消”按钮并获得零高度之后searchBar重新计算了其框架。尝试将searchBar初始化逻辑移到单独的方法,并在UISearchBarDelegate的searchBarCancelButtonClicked方法中也调用它。

我对您的建议也是:将部署目标设置为iOS 11,而不是使用searchBar进行操作,只需使用以下方法:

self.navigationItem.searchController = searchController