繁体   English   中英

当我点击搜索栏上的取消时,它消失了

[英]When I click cancel on the searchbar it disappears

我正在使用 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)

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

还有我对您的建议:将您的部署目标升级到 iOS 11,而不是使用 searchBar 进行操作,只需使用以下命令:

self.navigationItem.searchController = searchController

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM