【问题标题】:My UISearchBar moves to top (animated) --> want to disable我的 UISearchBar 移到顶部(动画)-> 想要禁用
【发布时间】:2016-09-21 08:24:49
【问题描述】:

我想在我的tableView 上实现一个UISearchBar

我的代码(在 viewDidLoad 中):

self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.autocapitalizationType = .None
self.searchController.searchBar.autocorrectionType = .No

self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false

self.tableView.tableHeaderView = self.searchController.searchBar

当我点击searchBar时,这个移到顶部,就像它想隐藏navigationBar一样:

我在很多帖子中搜索了答案,但没有任何效果。我想禁用此动画,以便 searchBar 不会移动。

【问题讨论】:

  • 试试这个 self.edgesForExtendedLayout = UIRectEdgeNone;
  • 感谢您的回答。我试过self.edgesForExtendedLayout = .Noneself.searchController.edgesForExtendedLayout = .None。结果是一样的。

标签: ios iphone swift uitableview uisearchbar


【解决方案1】:

您应该能够通过设置来防止这种情况:

self.searchController.hidesNavigationBarDuringPresentation = false

【讨论】:

  • 我想是的。我会这样做,但这会改变任何事情
【解决方案2】:

当您开始搜索时,您可以将UISearchController 视为模态显示。如果你有一个通常的UINavigationController 设置,这会很好,但是在像你这样更“定制”的 UI 中,你可能会遇到搜索控制器附加到错误视图等问题。

我建议不要在你的情况下使用UISearchController,而是使用一个单独的UISearchBar,用你的界面的其余部分初始化(可能在故事板中?),然后手动进行搜索。如果您不能简单地过滤您的内容,请实施UISearchBarDelegate 并为搜索结果添加您自己的UITableView

虽然看起来您有一个 tableView 变量 — 您可以简单地添加另一个属性,类似于您用作 UITableViewDataSource 的属性并在其中存储 过滤 数据。因此,每当您在UISearchBar 中有内容时,您只需在重新加载表视图数据时使用过滤 数据源。例如:

var data: [String]
var filteredData: [String]

然后在UITableViewDataSource中使用filteredData

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredData.count
}

...

然后在UISearchBarDelegate中做这样的事情:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    // Filter the data you have. For instance:
    filteredData = data.filter({$0.rangeOfString(searchText).location != NSNotFound})
    tableView.reloadData()
}

【讨论】:

  • 我改变了。我只使用没有控制器的searchBar。完美运行!谢谢
【解决方案3】:

将此行粘贴到呈现控制器的 viewDidLoad 中: self.extendedLayoutIncludesOpaqueBars = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多