Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单击“取消”按钮时隐藏searchController搜索栏_Ios_Swift_Uinavigationcontroller_Uisearchcontroller - Fatal编程技术网

Ios 单击“取消”按钮时隐藏searchController搜索栏

Ios 单击“取消”按钮时隐藏searchController搜索栏,ios,swift,uinavigationcontroller,uisearchcontroller,Ios,Swift,Uinavigationcontroller,Uisearchcontroller,我已经在viewController中实现了一个搜索控制器,它带有一个表视图和一个搜索控制器,搜索按钮录制时显示搜索栏,搜索按钮是UIBarButtonItem,搜索栏显示在导航控制器中,但我想在点击取消按钮时使其消失,我尝试过使用serarchBarDelegate协议,但在这里点击代码时什么也没发生 class NewsTVController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearch

我已经在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

        })
    }  
}
这里是带有搜索按钮和搜索栏的导航控制器的图片

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

由于我没有找到实现这一点的方法,我找到了这个解决方案

@IBAction func searchButtonTaped(sender:UIBarButtonItem){
        print("tap inside")
        self.navigationItem.titleView = searchController.searchBar
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(hideSearchBar(sender:)))
        //self.searchIcon.is
        //self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
       // searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

        //}
        //self.navigationItem.rightBarButtonItem.
    }
func hideSearchBar(sender:UIBarButtonItem) {
        print("hay que escpder")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(searchButtonTaped(sender:)))
        //logoImageView.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = nil// = self.logoImageView

            self.navigationItem.title = "Noticias"
            //self.logoImageView.alpha = 1
        }, completion: { finished in

        })
    }

boila搜索栏现在从导航控制器上消失了,搜索按钮起作用了,因为它链接到界面生成器中的SearchButton,因为我找不到实现这一点的方法,所以我找到了这个解决方案

@IBAction func searchButtonTaped(sender:UIBarButtonItem){
        print("tap inside")
        self.navigationItem.titleView = searchController.searchBar
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(hideSearchBar(sender:)))
        //self.searchIcon.is
        //self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
       // searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

        //}
        //self.navigationItem.rightBarButtonItem.
    }
func hideSearchBar(sender:UIBarButtonItem) {
        print("hay que escpder")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(searchButtonTaped(sender:)))
        //logoImageView.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = nil// = self.logoImageView

            self.navigationItem.title = "Noticias"
            //self.logoImageView.alpha = 1
        }, completion: { finished in

        })
    }
boila搜索栏现在从导航控制器中消失,搜索按钮可以正常工作,因为它链接到界面生成器中的搜索按钮