ios - UIBarButton 项目不会显示在导航栏中

标签 ios swift uinavigationbar uibarbuttonitem uinavigationitem

我在我的 View 中创建了一个导航栏,它应该左右各有 2 个按钮。问题是无论我做什么它都不会显示按钮/ View 。我该怎么做?这是到目前为止我的代码。

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!")
    })

最佳答案

根据对您问题的更改,您应该查看 UISearchController class .粗略地浏览一下文档让我相信,在您正确配置它之后,您可以将它推送到 View Controller 堆栈上,它就会像您想要的那样运行。


旧答案:

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

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

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

这是我的一个项目中 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 的 View 通常不是一个好主意。

关于ios - UIBarButton 项目不会显示在导航栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043004/

相关文章:

ios - Swift:如何在设备旋转后刷新 UICollectionView 布局

ios - 如何使用动画在 UINavigationBar 上切换 View ?

iphone - 如何动态调整导航栏中的标题大小

ios - 设置导航栏以覆盖模态转场中的状态栏

ios - 允许 iPhone 6 Plus 横向,但不允许其他 iPhone

objective-c - 正在跳过详细 View Controller 中的第一个方法

ios - 如果 swift 按下 NSButton,则创建简单的 Action

ios - BLE 设备不断连接/断开连接

Swift/SpriteKit - 碰撞和对象

ios - 由于 textView 无法选择 tableView Cell