我在做导航栏中的搜索栏。编辑时,导航栏的右侧没有“取消”按钮。
尝试过:
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
}
并在“取消”处理程序中:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句