迅捷-xcode-NavigationBarItem-将图像更改为文本

时间:2019-02-13 11:54:42

标签: swift xcode uinavigationcontroller uinavigationbar

我用前面的NavigationController创建了一个新的ViewController。现在,我在navigationBar中获得此演示文稿。这是来自视图控制器之前的任何图像。我没有在那里设置此图像。如何将其更改为xcode中的文本?

enter image description here

在情节提要中,我看到以下内容: enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用ViewDidLoad中的以下代码来实现

        let imageBack = UIImage(named: "ic_back")!
        let btnLeftMenu = UIButton(type: .system)
        btnLeftMenu.bounds = CGRect(x: 10, y: 0, width: imageBack.size.width, height: imageBack.size.height)
        btnLeftMenu.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
//        btnLeftMenu.addTarget(self, action: #selector(self.onBtnBackTap(_:)), for: .touchUpInside)
        btnLeftMenu.setImage(imageBack, for: UIControl.State())
        btnLeftMenu.setTitle("Cancel", for: .normal)
        let leftButton = UIBarButtonItem(customView: btnLeftMenu)
        self.navigationItem.leftBarButtonItem = leftButton

ic_back 是后退箭头图像的图像名称