本文介绍了搜索栏 - 在导航栏取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做导航栏搜索栏。当我编辑我没有在导航栏右侧的取消按钮。
试过:
FUNC searchBarTextDidBeginEditing(搜索栏:的UISearchBar){ VAR barButton =的UIBarButtonItem(标题:取消,风格:UIBarButtonItemStyle.Done,目标:自我,行动:)
self.navigationItem.rightBarButtonItem = barButton
}
编辑:
我添加了搜索栏导航栏是这样的:
懒VAR搜索栏:=的UISearchBar的UISearchBar(帧:CGRectMake(0,0,300,20)) searchBar.placeholder =Hľadať
searchBar.barStyle = UIBarStyle.BlackTranslucent
VAR leftNavBarButton =的UIBarButtonItem(customView:搜索栏)
self.navigationItem.leftBarButtonItem = leftNavBarButton
解决方案
做到这一点是这样的:
FUNC searchBarShouldBeginEditing(搜索栏:的UISearchBar) - GT;布尔{
VAR cancelSearchBarButtonItem =的UIBarButtonItem(标题:取消,风格:UIBarButtonItemStyle.Plain,目标:自我,动作:cancelBarButtonItemClicked)
self.navigationItem.setRightBarButtonItem(cancelSearchBarButtonItem,动画:真)
返回true
}
而在取消处理程序:
FUNC searchBarCancelButtonClicked(搜索栏:的UISearchBar){
//关闭键盘
searchBar.resignFirstResponder() //如果您使用的是搜索控制器
// self.searchDisplayControllerCustom.setActive(假,动画:真) //删除取消按钮
self.navigationItem.setRightBarButtonItem(零,动画:真)
}FUNC cancelBarButtonItemClicked(){
self.searchBarCancelButtonClicked(self.searchBar)
}
和设置正确的颜色导航栏标题文字:
//这将设置文本颜色为应用程序的所有导航栏
//在启动应用程序的委托执行此操作
UINavigationBar.appearance()titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
和添加代理:
searchBar.delegate =自
I'm doing Search Bar in Navigation Bar. When I'm editing I don't have Cancel Button on right side of navigation Bar.
Tried:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var barButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: "")
self.navigationItem.rightBarButtonItem = barButton
}
EDIT:
I added search bar to nav bar like this:
lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 300, 20))
searchBar.placeholder = "Hľadať"
searchBar.barStyle = UIBarStyle.BlackTranslucent
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
解决方案
Do it in this way:
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
var cancelSearchBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelBarButtonItemClicked")
self.navigationItem.setRightBarButtonItem(cancelSearchBarButtonItem, animated: true)
return true
}
And in the "Cancel" handler:
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// closes the keyboard
searchBar.resignFirstResponder()
// If you are using a search controller
// self.searchDisplayControllerCustom.setActive(false, animated: true)
// remove the cancel button
self.navigationItem.setRightBarButtonItem(nil, animated: true)
}
func cancelBarButtonItemClicked() {
self.searchBarCancelButtonClicked(self.searchBar)
}
And set the correct color for the navigation bar title text:
// This sets the textcolor for all navigation bars in the app
// Do this in the app delegate on startup
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
And add the delegate:
searchBar.delegate = self
这篇关于搜索栏 - 在导航栏取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!