简体   繁体   English

搜索栏作为tableview中的标题-显示和消失

[英]Search bar as header in tableview - appear and disappear

I need to put a search bar at the top of my tableview. 我需要在表格视图的顶部放置一个搜索栏。 I am making a network call and when the results are greater than 100 I want to search bar to appear and when they are less than 100 I don't want to search bar to appear. 我正在打网络电话,当结果大于100时,我想显示搜索栏,而当结果小于100时,我不希望显示搜索栏。 The tableview is on the right side of the VC and does not take up the whole view controller. tableview位于VC的右侧,不占用整个视图控制器。 I want the search bar to be at the top of the table view as a header. 我希望搜索栏作为标题位于表格视图的顶部。

I cannot use a search controller because in iOS 11, using a search controller makes the search bar pop to the top of the VC when it is active. 我无法使用搜索控制器,因为在iOS 11中,使用搜索控制器会使搜索栏处于活动状态时弹出到VC的顶部。

I tried to set the tableviewheader to nil to get it to disappear. 我试图将tableviewheader设置为nil使其消失。 But I can't get it back obviously because I made the header nil. 但是我显然无法将其取回,因为我将标头设为零。

self.rightDetailFilterTableView.tableHeaderView = nil
self.rightDetailFilterTableView.sectionHeaderHeight = 0

I have put the search bar into the storyboard as seen in the image below. 我将搜索栏放入情节提要中,如下图所示。 Is this the right way to add the search bar as a header? 这是将搜索栏添加为标题的正确方法吗?

在此处输入图片说明

What is the best way to get it to appear and disappear in the tableview? 使它在表格视图中显示和消失的最佳方法是什么? I have tried a bunch of different methods. 我尝试了很多不同的方法。 They all either leave a blank header or do something else that causes problems. 它们要么留空标题,要么做其他可能导致问题的事情。 I also tried using the header delegate methods but that still did not work. 我也尝试使用标头委托方法,但仍然无法正常工作。

I am not using a tableview controller, I am using a normal VC. 我没有使用tableview控制器,而是在使用普通的VC。 I am also not using a search bar controller because of issues it causes in iOS 11. 由于它在iOS 11中引起的问题,我也没有使用搜索栏控制器。

Here's what I've done in one of my recent project. 这是我在最近的一个项目中所做的。 First, laid out my views like so: 首先,这样布置我的观点:

在Xib中查看布局

That is, the Search Bar was added to the parent view rather than the table view. 也就是说,搜索栏已添加到父视图而不是表视图。 This allows me to hide/show it as needed. 这使我可以根据需要隐藏/显示它。

Next, I've defined two optional layout constraint, one ensuring that the tableview is aligned to the top of the safe area, priority 750; 接下来,我定义了两个可选的布局约束,一个约束确保表格视图与安全区域顶部(优先级750)对齐; the other aligning the top of the search bar to the top of the safe area; 另一个将搜索栏的顶部与安全区域的顶部对齐; priority lower than 750 to hide it below the nav bar or priority higher than 750 to reveal it and push the table view down. 低于750的优先级可将其隐藏在导航栏下方,或高于750的优先级可将其显示在导航栏下方。

In code, I created a @IBOutlet to the layout constraint for the search bar to the top of the safe area, and I change its priority as needed: 在代码中,我为安全区域顶部的搜索栏的布局约束创建了一个@IBOutlet,并根据需要更改其优先级:

@IBAction
func toggleSearchBar(_ sender: Any?) {
    if searchBarVisibleLayoutConstraint.priority.rawValue > 750.0 {
        searchBarVisibleLayoutConstraint.priority = UILayoutPriority(rawValue: 1.0)
        searchBar?.endEditing(true)
    } else {
        searchBarVisibleLayoutConstraint.priority = UILayoutPriority(rawValue: 999.0)
    }
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

In my case, the navigation bar is opaque and the search bar is not visible behind it. 就我而言,导航栏是不透明的,而搜索栏在其后面是不可见的。 Your case may be different so you may also want to either clip the parent view or alpha fade the search bar when it is not visible. 您的情况可能有所不同,因此您可能还希望修剪父视图或在不可见时使Alpha淡入搜索栏。

Good luck! 祝好运!

Please check : 请检查 :

Created IBOutlet for my SearchBar . 为我的SearchBar创建了IBOutlet。

@IBOutlet weak var testbar: UISearchBar!

And in my viewDidLoad : 在我的viewDidLoad

override func viewDidLoad() {
    var contentOffset = tableView.contentOffset
    let showSearchBar = (results.count > 100)
    self.tableView.tableHeaderView?.isHidden = !(showSearchBar)
    if showSearchBar {
        contentOffset.y -= testbar.frame.size.height
    } else {
        contentOffset.y += testbar.frame.size.height
    }
    tableView.contentOffset = contentOffset
}

Here is my tableview storyboard 这是我的tableview故事板 在此处输入图片说明

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

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