我希望自定义导航项为facebook
应用。
有两个按钮和一个UISearchBar
。按钮为leftBarButtonItem
和rightBarButtonItem
。 UISearchBar
是titleView
。
要使标题视图中的搜索栏变宽,我删除了两个按钮,将其设置为nil,如下图所示。然后我点击searchBarCancelButton
时再次添加两个按钮,但按钮没有显示。
你帮我吗?
点击searchBarCancelButton
时,我希望它显示如下
我试过的代码:
func updateSearchResultsForSearchController(searchController: UISearchController) {
if navigationItem.rightBarButtonItem != nil {
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
}
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
navigationItem.setLeftBarButtonItem(getBarButtonItem("info", buttonfor: "left"), animated: false)
navigationItem.setRightBarButtonItem(getBarButtonItem("more_icon", buttonfor: "right"), animated: false)
}
extension MainviewCtrl : UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
if navigationItem.rightBarButtonItem != nil {
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
}
}
}
extension MainviewCtrl : UISearchBarDelegate {
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
navigationItem.setLeftBarButtonItem(getBarButtonItem("info", buttonfor: "left"), animated: false)
navigationItem.setRightBarButtonItem(getBarButtonItem("more_icon", buttonfor: "right"), animated: false)
}
}
class MainviewCtrl: UIViewController , UISearchControllerDelegate {
lazy var searchController : UISearchController = {
let _searchController = UISearchController(searchResultsController: nil)
_searchController.searchResultsUpdater = self
_searchController.dimsBackgroundDuringPresentation = false
_searchController.hidesNavigationBarDuringPresentation = false
_searchController.searchBar.delegate = self
_searchController.searchBar.tintColor = UIColor.whiteColor()
_searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
var textFieldInsideSearchBar = _searchController.searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
return _searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = searchController.searchBar
definesPresentationContext = true
addBarButtonItems()
}
func addBarButtonItems(){
navigationItem.leftBarButtonItem = getBarButtonItem("info", buttonfor: "left")
navigationItem.rightBarButtonItem = getBarButtonItem("more_icon", buttonfor: "right")
}
func getBarButtonItem(imagename : String , buttonfor : String)-> UIBarButtonItem!{
let image = (UIImage(named: imagename))!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
switch buttonfor.lowercaseString {
case "left":
return (UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(MainviewCtrl.infoAction)))
case "right":
return (UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(MainviewCtrl.moreAction)))
default : break ;
}
return UIBarButtonItem()
}
答案 0 :(得分:0)
updateSearchResultsForSearchController
并生成nil
项。您可以检查搜索控制器是否处于活动状态,然后将其设置为nil
中的updateSearchResultsForSearchController
:
func updateSearchResultsForSearchController(searchController: UISearchController) {
if navigationItem.rightBarButtonItem != nil && searchController.active {
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
}
}