ios - UISearchController 中的取消按钮导致 SearchBar 故障

标签 ios swift uitableview user-interface search

viewDidload 中,我实例化 UISearchController 并将其搜索栏添加到 TableView 的标题 View 中,这是标准过程。

如果我点击搜索栏并输入一些文本,则会显示结果,我可以在此处取消搜索,没有问题。

如果我选择其中一个结果行,插入下一个屏幕,弹出并在此处取消,搜索栏就会出现故障。

这是我的搜索 Controller :

// MARK : SearchController ! //

    var searchController : UISearchController!

    var resultsController = UITableViewController()

    ///////////
    override func viewDidLoad() {
        super.viewDidLoad()

        definesPresentationContext = true
        self.resultsController.tableView.dataSource = self
        self.resultsController.tableView.delegate = self
        self.searchController = UISearchController(searchResultsController: self.resultsController)
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.searchResultsUpdater = self
        self.searchController.dimsBackgroundDuringPresentation = true
        self.searchController.searchBar.sizeToFit()
        self.searchController.searchBar.barTintColor = UIColor.blackColor()
        self.searchController.searchBar.endEditing(true)
        self.searchController.searchBar.placeholder = "חפש ברים"
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.searchBar.delegate = self

}

整个搜索栏出现故障的 UI 示例:

enter image description here

就像搜索栏不再是tableHeaderView一样,它破坏了tableview将最后一个单元格和第一个单元格切成两半。

tableView 的常规外观示例:

enter image description here

提前谢谢您!

最佳答案

在尝试了大量设置后,我自己设法找到了解决方案,我不知道为什么这些特定设置可以解决问题,但以下是为我解决问题的方法:

属性检查器中的设置:

enter image description here

viewDidLoad 中的设置:

    override func viewDidLoad() {
                    super.viewDidLoad()
            
            //MARK : Fixes SearchBar Bugs
        
    self.resultsController.edgesForExtendedLayout = .All
    self.searchController.edgesForExtendedLayout = .All            

self.resultsController.extendedLayoutIncludesOpaqueBars = true
                    
// End of Bug Fixes.

关于ios - UISearchController 中的取消按钮导致 SearchBar 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41504038/

相关文章:

ios - 我可以使用多个自定义 URL 方案来使用 Facebook iOS SDK 进行身份验证吗

ios - UITableViewCell 中的动画立即完成

ios - textfieldshouldclear : not invoked by UITextfield

swift - 查找字符串中第 N 个单词的范围

ios - 有附加内容时 ScrollView 顶部/底部的渐变

ios - 我需要实现导航 Controller 来制作 UISearchViewController 吗?

ios - 由于添加了文本字段,警报 View 向上移动

javascript - 从 native Webview 获取网页的动态内容

ios - 如何获取 UITableView 中可编辑单元格的值? swift

ios - 如何将 UITableViewCell 显示为选中状态并仍然接收 didDeselectRowAtIndexPath