ios - 即使将样式更改为 UITableViewStyleGrouped 后,Tableview 标题也不可滚动

标签 ios swift uitableview swift4 xcode9

我正在将一个 searchBar 添加到我的 UITableView 的标题中,并希望能够在用户滚动单元格时使用它。

我已经尝试了所有方法,例如将样式设置为 UITableViewStyleGrouped 但它并没有解决我的问题。

这是代码

var searchController: UISearchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar

标题停留在停止位置的任何原因?

最佳答案

如果您使用的是 iOS 11,则可以使用以下代码将 UISearchBar 放入 UINavigationBar。即使您滚动 UITableView,UISearchBar 也将始终可见。

class DishesTableViewController : UITableViewController, UISearchResultsUpdating {

    func updateSearchResults(for searchController: UISearchController) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let searchController = UISearchController(searchResultsController: self)
        searchController.searchResultsUpdater = self

        self.navigationItem.searchController = searchController
        self.navigationItem.hidesSearchBarWhenScrolling = false 
    }

enter image description here

关于ios - 即使将样式更改为 UITableViewStyleGrouped 后,Tableview 标题也不可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46478793/

相关文章:

ios - 在scrollView中跳转到某个页面

ios - iOS推送通知消息

ios - Swift NsdateFormatter 问题

iphone - 使用数据库的新信息更新数组

iphone - 问题 : subview becoms on top of superview when it moves out of the superview

iphone - UITextField 在 UITableView 中滚动时清除 - 自定义单元格

ios - 无法在通知服务扩展中使用 pod

ios - 即使我添加了委托(delegate),iAd 也无法正常工作

ios - 在 Swift 2 中解析 JSON 时出现 AutoLayout 错误

ios - 我的应用程序因 'Could not cast value of type x to ' UITableViewdelegate' 而崩溃