简体   繁体   English

在viewDidLoad之后在NavigationBar中添加searchBar?

[英]Add searchBar in navigationBar after viewDidLoad?

In my application, I want to have a search button in the navigation bar and when the button is pressed searchbar should appear in the Navigation Bar. 在我的应用程序中,我希望导航栏中有一个搜索按钮,当按下按钮时,搜索栏应出现在导航栏中。 I've seen many posts on the subject, but somehow it does not work for me. 我已经看过很多关于该主题的帖子,但是不知怎么对我不起作用。 So, I placed a SearchDisplayController right below nav bar. 因此,我在导航栏下方放置了一个SearchDisplayController then in the viewDidLoad I assign an action to the search button and remove searchbar from superview. 然后在viewDidLoad ,将操作分配给搜索按钮,并从超级视图中删除搜索栏。 Once button is clicked, I am calling mapSearchDisplayController.displaysSearchBarInNavigationBar = true , but nothing happends. 单击按钮后,我正在调用mapSearchDisplayController.displaysSearchBarInNavigationBar = true ,但是什么也没发生。 Code extracts below: 下面的代码摘录:

    override func viewDidLoad() {
            super.viewDidLoad()

            banner.delegate = self

            // Add button to navbar
            var filterButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Organize, target: self, action: nil)
            self.navigationItem.leftBarButtonItem = filterButton

            var aboutButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "aboutAction")
            var searchButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "searchAction")
            var rButtons : [UIBarButtonItem] = [aboutButton, searchButton]
            self.navigationItem.rightBarButtonItems = rButtons


            // Deal with Search Display Controller
            self.mapSearchDisplayController.delegate = self.mapSearchDisplayController;
            self.mapSearchDisplayController.searchResultsDataSource = self.mapSearchDisplayController;
            self.mapSearchDisplayController.searchResultsDelegate = self.mapSearchDisplayController;
            self.mapSearchDisplayController.searchBar.removeFromSuperview();
}
    func searchAction(){
            println("Search action")
            mapSearchDisplayController.displaysSearchBarInNavigationBar = true
        }

If I call displaysSearchBarInNavigationBar = true in viewDidLoad then searchBar correctly appears in the Navigation Bar. 如果我在viewDidLoad调用displaysSearchBarInNavigationBar = true ,则searchBar正确显示在导航栏中。 How do I make appear in Nav bar on the button press? 如何使我在按下按钮时显示在导航栏中? Any help is appreaciated. 任何帮助表示赞赏。

I created a search bar using 我使用创建了一个搜索栏

let searchBar = UISearchBar(frame : CGRect(0, 0, self.navigationController?.navigationBar.frame.size.width)! * 0.9, 0)

self.searchBar.placeholder = "Search"
self.searchBar.delegate = self

And then added it to the navigation bar as follows: 然后将其添加到导航栏中,如下所示:

self.navigationItem.titleView = self.searchBar

Further styling etc can also be done but I just created and added it to the navbar on button pressed and removed it from superview when it needed to be hidden. 还可以进行其他样式设置,但我只是在将按钮隐藏时将其创建并添加到导航栏中,并从超级视图中将其删除。

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

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