在Swift的导航栏中获取搜索栏 [英] Get search bar in navigation bar in Swift

查看:390
本文介绍了在Swift的导航栏中获取搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试了将搜索栏放到Swift导航栏中的所有内容。但遗憾的是我还没有让它发挥作用,只是......

So I've tried everything trying to get a search bar into the navigation bar in Swift. But sadly I haven't gotten it working, just yet...

对于那些不知道我在说什么的人,我正在尝试做这样的事情

For those of you who don't know what I'm talking about, I'm trying to do something like this

请注意导航栏中的搜索栏。所以我现在正在使用

Note the search bar in the navigation bar. So here's what I'm currently using

self.searchDisplayController?.displaysSearchBarInNavigationBar = true

我在我的 viewDidLoad 中弹出,然后当我加载应用程序时我就是提出,只是一个空的导航栏.... :(任何想法?

I popped that in my viewDidLoad, and then when I load up the app I'm presented with, just an empty navigation bar.... :( Any ideas?

推荐答案

试试这个

var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
  self.navigationItem.leftBarButtonItem = leftNavBarButton

更新

您保留了懒惰的UISearchBar属性

You keep a lazy UISearchBar property

lazy   var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))

在viewDidLoad中

In viewDidLoad

searchBar.placeholder = "Your placeholder"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton

如果你想使用storyboard
只需将你的搜索栏拖动为插座,然后更换您的插座搜索栏的懒惰属性

If you want to use storyboard just drag your searchbar as a outlet,then replace the lazy property with your outlet searchbar

这篇关于在Swift的导航栏中获取搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆