如何在Swift中更改UIButton图像


111

我正在尝试使用Swift更改UIButton的图像...我该怎么办

这是OBJ-C代码。但是我不知道Swift:

[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

Answers:


320

从您的Obc-C代码中,我认为您想为按钮设置图片,因此请尝试以下方式:

let playButton  = UIButton(type: .Custom)
if let image = UIImage(named: "play.png") {
    playButton.setImage(image, forState: .Normal)
}

简而言之:

playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)

对于Swift 3:

let playButton  = UIButton(type: .custom)
playButton.setImage(UIImage(named: "play.png"), for: .normal)

7
您应该将buttonWithType设置为UIButtonType.Custom,而不是System,直到图像显示为止
sazz

8
对于寻求Swift 3语法的用户: playButton.setImage(UIImage(named: "play.png"), for: .normal)
Dustin Senos '16

11

在Swift 4中,(Xcode 9)示例将按钮的图片打开或关闭(btnRec):

var bRec:Bool = true

@IBOutlet weak var btnRec: UIButton!
@IBAction func btnRec(_ sender: Any) {
    bRec = !bRec
    if bRec {
        btnRec.setImage(UIImage(named: "MicOn.png"), for: .normal)
    } else {
        btnRec.setImage(UIImage(named: "MicOff.png"), for: .normal)
    }
}

9

假设这是你的联系UIButton Name方式

@IBOutlet var btn_refresh: UIButton!

您可以直接在三种模式下放置图像

 // for normal state
btn_refresh.setImage(UIImage(named: "xxx.png"), forState: UIControlState.Normal)
     // for Highlighted state
      btn_refresh.setImage(UIImage(named: "yyy.png"), forState: UIControlState.Highlighted)

        // for Selected state
         btn_refresh.setImage(UIImage(named: "zzzz.png"), forState: UIControlState.Selected)

按一下按钮

  //MARK: button_refresh action
@IBAction func button_refresh_touchup_inside(sender: UIButton)
{
    //if you set the image on same  UIButton
    sender.setImage(UIImage(named: "newimage.png"), forState: UIControlState.Normal)

    //if you set the image on another  UIButton
       youranotherbuttonName.setImage(UIImage(named: "newimage.png"), forState: UIControlState.Normal)
}

如果对按钮有操作,则无需将按钮设置为插座。button参数sender是对按钮的引用,因此您可以只执行“ let yourButton = sender as!UIButton”,然后根据需要对按钮进行更改。
米卡蒙托亚

7

对于使用Assets.xcassets和Swift 3的任何人来说,都是这样(不需要.png)

let buttonDone  = UIButton(type: .Custom)

if let image = UIImage(named: "Done") {
    self.buttonDone.setImage(image, for: .normal)
}

6

从swift 3.0开始,正常状态已被删除。您可以使用以下方法应用正常状态。

myButton.setTitle("myTitle", for: [])

5

我更喜欢首先在顶部初始化变量的方法:

let playButton:UIButton!
var image:UIImage!

然后在viewDidLoad中设置它们

override func viewDidLoad {
  ...
  playButton = UIButton(type: .Custom)
  imagePlay = UIImage(named:"play.png")
  playButton.setImage(imagePlay, forState: .Normal)
}

4

是的,甚至我们也可以通过使用标志来更改UIButton的图像。

class ViewController: UIViewController
{
    @IBOutlet var btnImage: UIButton!
    var flag = false

    override func viewDidLoad()
    {
        super.viewDidLoad()

        //setting default image for button
        setButtonImage()

    }

    @IBAction func btnClick(_ sender: Any)
    {
        flag = !flag
        setButtonImage()
    }

    func setButtonImage(){
        let imgName = flag ? "share" : "image"
        let image1 = UIImage(named: "\(imgName).png")!
        self.btnImage.setImage(image1, for: .normal)
    }

}

在这里,每次单击后,您的按钮图像都会交替更改。


2

Swift 5并确保图像缩放到按钮的大小,但保持在按钮范围之内。

yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill

// Use setBackgroundImage or setImage
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)

1

您实际上可以通过突出显示按钮来执行此操作,并在右侧工具栏上的insepctor中可以更新图像。显然,您也可以按照前面所述在代码中进行操作,但这是您的另一种选择


这实际上是行不通的。您在哪里更新映像?如果您要谈论的是不同的状态,例如突出显示,选定等,那么它在那里不起作用,
KFDoom

1

在Swift 4.2和Xcode 10.1中

所选的UIButton添加图像

button.addTarget(self, action: #selector(self.onclickDateCheckMark), for: .touchUpInside)//Target 
button.setImage(UIImage.init(named: "uncheck"), for: UIControl.State.normal)//When selected
button.setImage(UIImage.init(named: "check"), for: UIControl.State.highlighted)//When highlighted
button.setImage(UIImage.init(named: "check"), for: UIControl.State.selected)//When selected

但是,如果要更改选定的按钮图像,则需要更改其选定状态。然后,只有选定的图像会出现在您的按钮中。

@objc func onclickDateCheckMark(sender:UIButton) {
    if sender.isSelected == true {
        sender.isSelected = false
        print("not Selected")
    }else {
        sender.isSelected = true
        print("Selected")

    }
}

1

迅捷5

yourButton.setImage(UIImage(named: "BUTTON_FILENAME.png"), for: .normal)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.