导航栏按钮和项目未在 swift 3 中显示 [英] navigation bar button and items not showing in swift 3

查看:22
本文介绍了导航栏按钮和项目未在 swift 3 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 swift 中以编程方式使用导航栏,但无法在导航栏中显示栏按钮项,这是我所做的代码

am using navigation bar programmatically in swift, but am not able to show the bar button items in navigation bar, this is the code what I did

override func viewDidLoad() {
        super.viewDidLoad()

  let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: 420, height: 65))
        self.view.addSubview(navBar)

        navBar.backgroundColor = hexStringToUIColor("4DC8BD")

        let navigationItem = UINavigationItem()

       self.title = "Transport APP"

        let btn1 = UIButton(type: .custom)
        btn1.setImage(UIImage(named: "Menu1"), for: .normal)
        btn1.frame = CGRect(x: 30, y: 30, width: 30, height: 30)
        btn1.addTarget(self, action: #selector(HomeViewController.menubuttonclick(_:)), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn1)


         self.navigationItem.setRightBarButtonItems([item1], animated: true)

}

    @IBAction func menubuttonclick(_ sender:UIBarButtonItem )
    {

        print("this menu button click")

    }

我可以尝试很多方法,但没有得到结果如何在导航栏中显示显示栏按钮项,

I can try many ways but am not getting the results how to show show bar button item in navigation bar,

推荐答案

您应该将 UINavigationItem 添加到您的 UINavigationBar 并且在 item1 中需要加入navitem 看下面的代码

You should add UINavigationItem to your UINavigationBar and in item1 need to be added in navitem Look at below code

let navitem = UINavigationItem()
navitem.rightBarButtonItem = item1
navBar.setItems([navitem], animated: true)

这篇关于导航栏按钮和项目未在 swift 3 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆