【问题标题】:How to use Search bar with Navigaitonbar如何将搜索栏与 Navigaitonbar 一起使用
【发布时间】:2018-03-14 04:10:38
【问题描述】:

我已经成功地让 UISearchController 在一个新项目中工作,但是当我尝试在我自己的项目中做同样的事情时,它的行为会有所不同。

这就是我为使其正常工作所做的工作。

故事板:

这是我的视图控制器代码:

class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var searchController : UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()

        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let modalVC =  mainStoryboard.instantiateViewController(withIdentifier: "SearchTableViewController_SBID")
        self.searchController = UISearchController(searchResultsController:  modalVC)

        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self

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

        self.navigationItem.titleView = searchController.searchBar

        self.definesPresentationContext = true
    }

    func updateSearchResults(for searchController: UISearchController) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我得到的结果是,当我点击搜索栏时,背景会变暗,而当我开始输入时,tableview 控制器会可见,会不会搜索栏在导航栏中仍然可见表格视图控制器。

但是,当我尝试在我的项目中实现相同的东西时,当我开始在搜索栏上输入时,表格视图控制器将填满整个屏幕,并且导航栏不可见。

这是我的项目的故事板:

请注意,这两个项目是相似的,除了其中一个是Navigation Controller -> Tab Bar View Controller,当我在搜索栏上键入时它不显示导航栏。

如何在项目Navigation Controller -> Tab Bar View Controller 中显示导航栏?

这是 2 个项目的样子:

【问题讨论】:

    标签: ios swift uisearchcontroller uisearchdisplaycontroller


    【解决方案1】:

    故事板设置

    两个视图控制器的编码

    class ViewController1: UIViewController,UISearchBarDelegate {
        @IBOutlet weak var lbl1: UILabel!
        var searchBar:UISearchBar? = nil
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Code to identify the searchbar
            for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
            {
                if view.isKind(of: UISearchBar.self)
                {
                    searchBar = (view as! UISearchBar);
                }
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
    
            //Assign Delegate and blank the search bar text 
            searchBar?.text = ""
            searchBar?.delegate = self
    
        }
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            lbl1.text = "Tab 1 : \(searchText)"
    
        }
    
    }
    class ViewController2: UIViewController,UISearchBarDelegate {
        @IBOutlet weak var lbl1: UILabel!
        var searchBar:UISearchBar? = nil
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Code to identify the searchbar
            for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
            {
                if view.isKind(of: UISearchBar.self)
                {
                    searchBar = (view as! UISearchBar);
                }
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            searchBar?.text = ""
            searchBar?.delegate = self
        }
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            lbl1.text = "Tab 2 : \(searchText)"
    
        }
    
    }
    

    结果

    【讨论】:

    • 我已经有了这一行:self.searchController = UISearchController(searchResultsController: modalVC),它在一个干净的项目中工作,只是在一个带有 TabBar 控制器的项目中没有工作
    • 标签栏控制器的关键你已经将每个标签嵌入导航控制器,就像我更新的图像一样
    • 你只是反转,只是将每个视图控制器从标签栏嵌入到导航视图控制器
    • 我想你误会了我。我实际上在使用标签栏时显示导航栏有问题,里面有一个搜索栏,不像你编辑我的标题(如何使用带有标签栏的导航栏)。我在整个项目中使用带有标签栏的导航栏没有问题。
    • 如果像你说的我的标签栏的每个视图控制器我必须嵌入导航控制器,这是否意味着如果我有 5 个标签,我需要为每个标签有 5 个搜索栏?
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 2021-10-29
    • 2021-11-08
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多