最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift在NavigationBar上面添加图片,文字,按钮等内容

Swift crifan 1462浏览 0评论

需要在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)

添加出来对应的按钮了:

added button on navigation bar

另外,再去添加其它按钮:

        //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)

即可添加到几个按钮了:

added rest text and label and image to navi bar

然后接着去解决:

[已解决]swift中UIBarButtonItem添加customView的Button的内容显示位置异常

转载请注明:在路上 » [已解决]swift在NavigationBar上面添加图片,文字,按钮等内容

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.084 seconds, using 18.83MB memory