UIBarButtonItems不会显示在导航栏中

时间:2014-11-20 15:14:52

标签: ios swift uinavigationbar uibarbuttonitem uinavigationitem

我在视图中创建了一个navigationBar,假设左右按住2个按钮。问题是无论我做什么都不会显示按钮/视图。我怎么想这样做?到目前为止,这是我的代码。

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()



    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Aerovias Brasil NF", size: 30)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]

    navTitleView = UIView(frame: CGRectMake(0, 0, 100, 44))
    titleLabel.text = "SHOPOP"
    navTitleView?.addSubview(titleLabel)
    self.navigationItem.titleView?.addSubview(navTitleView!)

    navigationHeight = UIApplication.sharedApplication().statusBarFrame.height+self.navigationController!.navigationBar.bounds.height

    searchBar = UISearchBar(frame: CGRectZero)
    searchBarWrapper = UINavigationBar(frame: CGRectMake(0, 100, self.navigationController!.navigationBar.bounds.size.width, self.navigationHeight!))
    var buttonSearchBar:UIBarButtonItem = UIBarButtonItem(customView: searchBar!)
    var cancelButton:UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
    searchBarWrapper?.topItem?.leftBarButtonItem = buttonSearchBar
    searchBarWrapper?.topItem?.rightBarButtonItem = cancelButton

    self.navigationController?.view.addSubview(searchBarWrapper!)


}


@IBAction func showSearchBar(sender: UIBarButtonItem) {


    searchBar?.becomeFirstResponder()
    UIView.animateWithDuration(0.25,  animations: {
        // Optional chaining may return nil
        self.searchBarWrapper!.center = CGPointMake(self.navigationController!.view.center.x, self.navigationHeight!/2)


        // return
        }, completion: {
            finished in
            println("Basket doors opened!")
    })

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func hideSearchBar(sender: UIBarButtonItem) {

    UIView.animateWithDuration(0.25,  animations: {
        // Optional chaining may return nil
        self.searchBarWrapper!.center = CGPointMake(self.navigationController!.view.center.x, -self.navigationHeight!/2)

        self.searchBar?.resignFirstResponder()

        // return
        }, completion: {
            finished in
            println("Basket doors opened!")
    })

1 个答案:

答案 0 :(得分:1)

根据您问题的更改,您应该查看UISearchController class。粗略地浏览文档会让我相信,在正确配置之后,您可以将其推送到视图控制器堆栈上,它的行为就像您想要的那样。


旧回答:

虽然我不确定在UIBarButtonItem中包装UISearchBar,但我可以肯定地说这是将UINavigationBar添加到UINavigationController的错误方法:

self.navigationController?.view.addSubview(searchBarWrapper!)

为导航栏提供项目的正确方法是覆盖UIViewController的navigationItem属性并返回自定义的UINavigationItem实例。这样,当您从UINavigationController的堆栈中推出和弹出视图控制器时,导航栏将自动更新。

这是我的一个项目中UIViewController子类的一个例子。

private var customNavigationItem = UINavigationItem(title:nil);

override var navigationItem:UINavigationItem
{
    get
    {
        customNavigationItem.title = self.title;
        customNavigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem:.Cancel,
            target:self, action:"dismissForm:");
        customNavigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem:.Save,
            target:self, action:"saveForm:");

        return customNavigationItem;
    }
}

这样做可以确保所有"免费" UINavigationController提供的功能按预期工作。 与UINavigationController的观点混淆通常是一个坏主意。