How to set image for bar button with swift?

前端 未结 14 1138
一个人的身影
一个人的身影 2020-11-28 04:22

I am trying to set an Image for bar button Item for that I have an image like:

\"enter

相关标签:
14条回答
  • 2020-11-28 05:28

    Swift 4.

    @IBOutlet weak var settingBarBtn: UIBarButtonItem! {
        didSet {
            let imageSetting = UIImageView(image: UIImage(named: "settings"))
            imageSetting.image = imageSetting.image!.withRenderingMode(.alwaysOriginal)
            imageSetting.tintColor = UIColor.clear
            settingBarBtn.image = imageSetting.image
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:28

    If your UIBarButtonItem is already allocated like in a storyboard. (printBtn)

        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        btn.setImage(UIImage(named: Constants.ImageName.print)?.withRenderingMode(.alwaysTemplate), for: .normal)
        btn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlePrintPress(tapGesture:))))
        printBtn.customView = btn
    
    0 讨论(0)
提交回复
热议问题