取消按钮cliked时隐藏searchController searchBar

时间:2017-07-10 14:01:50

标签: ios swift uinavigationcontroller uisearchcontroller

我在viewController中实现了一个带有表视图和搜索控制器的搜索控制器,当搜索按钮被录音时显示搜索栏,搜索按钮是UIBarButtonItem,搜索栏显示在导航控制器中但是我想要当点击取消按钮时消失,我尝试使用serarchBarDelegate协议,但没有任何事情发生在这里代码

    class NewsTVController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate{
let searchController = UISearchController(searchResultsController: nil)
    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.searchResultsUpdater = self
        definesPresentationContext = true
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = true

        self.searchController.hidesNavigationBarDuringPresentation = false

        loadNews()
    }


@IBAction func searchButtonTaped(sender:UIBarButtonItem){
        print("tap inside")
        self.navigationItem.titleView = searchController.searchBar
        //self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
       // searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

        //}
        //self.navigationItem.rightBarButtonItem.
    }
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        hideSearchBar()
    }
    func hideSearchBar() {
        print("hay que escpder")
        //navigationItem.setLeftBarButtonItem(searchBarButtonItem, animated: true)
        //logoImageView.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = nil// = self.logoImageView
            self.navigationItem.title = "Eventos"
            //self.logoImageView.alpha = 1
        }, completion: { finished in

        })
    }  
}

这里是带有搜索按钮和搜索栏的导航控制器的图片 enter image description here

我想要的是,当点击取消按钮时,导航栏保持在第一张图片中,没有搜索栏。 还有一些提示可以在搜索栏出现时隐藏后退按钮和搜索按钮。

1 个答案:

答案 0 :(得分:0)

因为我找不到一种方法来实现这个解决方案

travis

并且boila搜索栏现在从导航控制器消失,并且搜索按钮功能正常,因为它已链接到Interface Builder中的 searchButtonTaped