我正在尝试将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.