简体   繁体   English

按下取消按钮时隐藏搜索栏,并调整导航栏的大小

[英]Hide search bar when cancel button is pressed, and resize navbar

I have a search bar implemented using a UISearchController inside my navigation bar.我在导航栏中使用UISearchController实现了一个搜索栏。 There is also a table view whose top constraint is set to the bottom of the navigation bar.还有一个表格视图,它的顶部约束设置在导航栏的底部。

Desired behaviour: When the cancel button is pressed, the search bar is hidden and the top constraint of the table view returns to what it was before the search bar was removed (see screenshot #1 at the end of this post)期望的行为:当取消按钮被按下时,搜索栏被隐藏,表格视图的顶部约束恢复到搜索栏被删除之前的状态(见本文末尾的截图 #1

Current behaviour: When the cancel button is pressed, the search bar is gone but the top constraint of the tableView does not change in response (see screenshot #3 )当前行为:按下取消按钮时,搜索栏消失,但 tableView 的顶部约束不会响应更改(请参见屏幕截图 #3

A possible solution to this problem would be to manually update constraints whenever the cancel button is clicked.此问题的一个可能解决方案是在单击取消按钮时手动更新约束。 However, I can't find a way to access the tableView's constraints from the UISearchBarDelegate method searchBarCancelButtonClicked但是,我找不到从UISearchBarDelegate方法searchBarCancelButtonClicked访问 tableView 约束的方法

Code Snippet:代码片段:

class ViewController: UIViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.searchBar.delegate = self

        /* Adding search button to the navbar */

        /* setting tableView constraints */

        /* tableView delegate/datasource methods, etc... */
    } 

    @objc func searchButtonTapped(_ sender: UIBarButtonItem) {
        setup()
        navigationItem.searchController = searchController
    }

    func setup() {
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.sizeToFit()
    }
}

extension UISearchBarDelegate {
    public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

        navigationItem.searchController = nil

        /* Cannot access tableview constraints from here because extension is outside of the class */
    }
}

Before the search button is pressed.在按下搜索按钮之前。

Before the cancel button is pressed.在按下取消按钮之前。 在此处输入图片说明

After the cancel button is pressed.按下取消按钮后。 在此处输入图片说明

Add one line code as following:添加一行代码如下:

func searchBarCancelButtonClicked(_ searchBar: UISearchBar){

   self.navigationItem.searchController = nil

   self.view.setNeedsLayout()

 /* Cannot access tableview constraints from here because extension is outside of the class */
}

(YES this is right thing) (是的,这是正确的事情)

func searchBarCancelButtonClicked(_ searchBar: UISearchBar){

   self.navigationItem.searchController = nil

   self.view.setNeedsLayout()

 /* Cannot access tableview constraints from here because extension is outside of the class */
}

Try to update the view inside the navigation controller like this:尝试像这样更新导航控制器内的视图:

func searchBarCancelButtonClicked(_ searchBar: UISearchBar){
    navigationItem.searchController = nil // or searchController
    navigationController?.view.setNeedsLayout() // invalidate current layout 
    navigationController?.view.layoutIfNeeded() // force update layout
}

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

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