BarButtonItem在添加后不显示按钮

时间:2016-09-19 10:16:13

标签: iphone swift

我希望自定义导航项为facebook应用。 有两个按钮和一个UISearchBar。按钮为leftBarButtonItemrightBarButtonItemUISearchBartitleView。 要使标题视图中的搜索栏变宽,我删除了两个按钮,将其设置为nil,如下图所示。然后我点击searchBarCancelButton时再次添加两个按钮,但按钮没有显示。 你帮我吗? enter image description here

点击searchBarCancelButton时,我希望它显示如下 enter image description here

我试过的代码:

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()
}

1 个答案:

答案 0 :(得分:0)

点击取消按钮后会再次调用

updateSearchResultsForSearchController并生成nil项。您可以检查搜索控制器是否处于活动状态,然后将其设置为nil中的updateSearchResultsForSearchController

func updateSearchResultsForSearchController(searchController: UISearchController) {
    if navigationItem.rightBarButtonItem != nil && searchController.active {
        self.navigationItem.rightBarButtonItem = nil 
        self.navigationItem.leftBarButtonItem = nil
    }
}