搜索栏 - 导航栏中的取消按钮
Search Bar - Cancel Button in Navigation Bar
我正在导航栏中做搜索栏。当我编辑时,导航栏右侧没有取消按钮。
尝试过:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var barButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: "")
self.navigationItem.rightBarButtonItem = barButton
}
编辑:
我像这样将搜索栏添加到导航栏:
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
这样做:
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
}
并且在 "Cancel" 处理程序中:
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)
}
并为导航栏标题文本设置正确的颜色:
// 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()]
并添加代表:
searchBar.delegate = self
如果这是 Objective-C 答案:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES];
}
那么这可能是 Swift 答案:(不是 100% 确定)
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
记得link你的代理人。
我正在导航栏中做搜索栏。当我编辑时,导航栏右侧没有取消按钮。
尝试过:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var barButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: "")
self.navigationItem.rightBarButtonItem = barButton
}
编辑:
我像这样将搜索栏添加到导航栏:
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
这样做:
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
}
并且在 "Cancel" 处理程序中:
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)
}
并为导航栏标题文本设置正确的颜色:
// 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()]
并添加代表:
searchBar.delegate = self
如果这是 Objective-C 答案:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES];
}
那么这可能是 Swift 答案:(不是 100% 确定)
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
记得link你的代理人。