需要在NavigationBar上面添加图片,文字,按钮等内容
搜:
swift draw on navigationbar
swift custom navigation bar
参考:
搜:
swift uibarbuttonitem example
参考:
试了各种:
//4. add button let addImage = UIImage(named:"navi_add") // let addNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("toggleAddDropdownList")) let addNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(image: addImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("toggleAddDropdownList")) let searchImage = UIImage(named:"navi_search") let searchNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: Selector("jumpToSearch")) let mainNaviBarRightItems:[UIBarButtonItem] = [addNaviBarButtonItem, searchNaviBarButtonItem] // self.navigationController?.navigationItem.setRightBarButtonItems(mainNaviBarRightItems, animated: true) // //mainViewVC.navigationController?.navigationItem.leftBarButtonItem = switchTeamButton //self.navigationBar.backItem?.hidesBackButton = false //self.navigationItem.setRightBarButtonItems(mainNaviBarRightItems, animated: true) //self.navigationItem.setRightBarButtonItem(addNaviBarButtonItem, animated: true) self.navigationController?.navigationItem.setRightBarButtonItem(addNaviBarButtonItem, animated: true) self.navigationItem.setRightBarButtonItem(addNaviBarButtonItem, animated: true)
都没用。
后来才发现,之前是在AppDelegate:
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
或者NavigationController:
class MainNavigationController: UINavigationController { override func viewDidLoad() {
中调用navigationItem,是不行的
以及在ViewController的viewDidLoad中:
class MainViewController: UITabBarController, UIGestureRecognizerDelegate { override func viewDidLoad() {
但是却想要操作:self.navigationController
结果得到的是nil:
print("self.navigationController=\(self.navigationController)") //self.navigationController=nil
最后是在:
ViewController中的viewDidLoad操作self.navigationItem:
print("self.navigationItem=\(self.navigationItem)")//self.navigationItem=<UINavigationItem: 0x7f8c205132a0>
即可通过代码:
let addNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"navi_add"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("toggleAddDropdownList")) self.navigationItem.setRightBarButtonItem(addNaviBarButtonItem, animated: true)
添加出来对应的按钮了:
另外,再去添加其它按钮:
//use system add button //let addNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("toggleAddDropdownList")) //add custom image add button let addNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"navi_add"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("toggleAddDropdownList")) //self.navigationItem.setRightBarButtonItem(addNaviBarButtonItem, animated: true) let searchNaviBarButtonItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"navi_search"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("jumpToSearch")) let mainNaviBarRightItems:[UIBarButtonItem] = [addNaviBarButtonItem, searchNaviBarButtonItem] self.navigationItem.setRightBarButtonItems(mainNaviBarRightItems, animated: true) let switchTeamNaviBarButtonItem = UIBarButtonItem(customView: switchTeamButton) self.navigationItem.setLeftBarButtonItem(switchTeamNaviBarButtonItem, animated: true)
即可添加到几个按钮了:
然后接着去解决: