在swift中添加UIBarButton项

前端之家收集整理的这篇文章主要介绍了在swift中添加UIBarButton项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将UIBarButton项添加到导航栏.

这是我的导航栏类声明:

import UIKit

class NavigationBarController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        configureToolbar()        
    }

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

    // #pragma mark - Navigation bar data source

    func configureToolbar() {
        let toolbarButtonItems = [
            searchBarButtonItem
        ]
        toolbar.setItems(toolbarButtonItems,animated: true)
    }

    var searchBarButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: .Search,target: self,action: "barButtonItemClicked:")
    }
}

编译器没有错误,但我得到的只是一个简单的导航栏.

如何显示UIBarButtonItem?

要将项添加到NavigationController的NavigationBar或添加到ViewController的NavigationBar,您需要首先浏览NavigationItem.尝试这个:
self.navigationItem.setRightBarButtonItems(navigationBarButtonItemsArray,animated: true)

// Or if you just want to insert one item.

self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search,action: "barButtonItemClicked:"),animated: true)

要将按钮切换到左侧,只需将setRightBarButtonItem替换为setLeftBarButtonItem或setLeftBarButtonItems.

原文链接:https://www.f2er.com/swift/320224.html

猜你在找的Swift相关文章