简体   繁体   English

如何在Swift 3中设置UIButton的图像?

[英]How to set image of UIButton in Swift 3?

Originally, my code was working. 最初,我的代码正在运行。

IBAction func clickedon(_ sender: UIButton) {

    if(gamestate[sender.tag] == 0 && gameisactive == true){

        gamestate[sender.tag] = activeplayer

    if (activeplayer == 1){
        print("working")
        sender.setImage(UIImage(named:"Cross.png"), for: UIControlState()) <-----------
        activeplayer = 2
    }
    else {
        print("working2")
        sender.setImage(UIImage(named:"Nought.png"), for: UIControlState()) <-----------
        activeplayer = 1

    } 
    }

These two pieces of code that set image were working. 设置图像的这两段代码正在运行。 These buttons have nothing set to their image. 这些按钮没有设置任何图像。 When they are clicked on, I want them to switch to those images. 单击它们时,我希望它们切换到这些图像。 Those images are in the folder with the view controller. 这些图像位于带有视图控制器的文件夹中。 I am not sure why they stopped working. 我不确定他们为什么停止工作。 I commented out all other code to see if it could be an issue but it doesn't seem to be. 我评论了所有其他代码,看它是否可能是一个问题,但它似乎不是。 The print statements get printed out in both cases. 在两种情况下都会打印出打印报表。 It's just the set image that doesn't seem to do anything. 它只是设置的图像似乎没有做任何事情。 There is no error, it just does nothing. 没有错误,它什么都不做。

I don't really understand the UIControlState() code. 我真的不了解UIControlState()代码。 I am mimicking an online project that has the same code and it works. 我正在模仿一个具有相同代码的在线项目。 It is really strange. 真的很奇怪。 Please let me know if you have any suggestions! 如果您有任何建议,请告诉我!

IBAction func clickedon(_ sender: UIButton) { IBAction func clickedon(_ sender:UIButton){

if(gamestate[sender.tag] == 0 && gameisactive == true){

    gamestate[sender.tag] = activeplayer

if (activeplayer == 1){
    print("working")
   sender.setImage(UIImage(named: "Cross.png")!, forState: UIControlState.Normal)
   sender.setImage(UIImage(named:"CrossSelected.png")!, forState: UIControlState.Selected)

    activeplayer = 2
}
else {
    print("working2")
    sender.setImage(UIImage(named: "Nought.png")!, forState: UIControlState.Normal)
    sender.setImage(UIImage(named:"NoughtSelected.png")!, forState: UIControlState.Selected)
    activeplayer = 1

} 
}

You can try this : 你可以试试这个:

let image = UIImage(named: "imagename.png")
yourButton.setBackgroundImage(image, for: .normal)

I try using other setImage calls but not working. 我尝试使用其他setImage调用但不工作。 I found this setBackgroundImage to let the image appear. 我发现这个setBackgroundImage让图像出现。 Hope that it will helps you. 希望它能帮到你。

In your code, UIControlState() can be replaced with UIControlState.normal or UIControlState.highlighted . 在您的代码中,UIControlState()可以替换为UIControlState.normalUIControlState.highlighted

https://developer.apple.com/reference/uikit/uicontrolstate https://developer.apple.com/reference/uikit/uicontrolstate

IBAction func clickedon(_ sender: UIButton) {

    if(gamestate[sender.tag] == 0 && gameisactive == true){

        gamestate[sender.tag] = activeplayer

    if (activeplayer == 1){
        print("working")
        sender.setImage(UIImage(named:"Cross.png"), for: UIControlState.normal)
        sender.setImage(UIImage(named:"Cross_highlighted.png"), for: UIControlState.highlighted)
        activeplayer = 2
    }
    else {
        print("working2")
        sender.setImage(UIImage(named:"Nought.png"), for: UIControlState.normal)
        sender.setImage(UIImage(named:"Nought_highlighted.png"), for: UIControlState.highlighted)
        activeplayer = 1

    } 
}

Are you forcing a width and height constraint? 你强迫宽度和高度约束? If not your buttons may be degenerate because they have no image inside of them. 如果不是,您的按钮可能会退化,因为它们内部没有图像。 You can also set the frame when you set the image; 您也可以在设置图像时设置框架; I'd prefer autolayout. 我更喜欢自动布局。

  UIImage(named:"Cross.png")!
  sender.setImage(image, for: .normal)
  sender.frame.size = image.size

I figured out the answer. 我想出了答案。 The UIControlState.normal works, the issue was that the buttons were actually placed below my board. UIControlState.normal工作,问题是按钮实际上放在我的板下面。 This meant I could click on them yet they wouldn't appear. 这意味着我可以点击它们但它们不会出现。

It explains why the print statements and everything worked without error. 它解释了为什么打印语句和所有内容都能正常运行。 Thank you! 谢谢!

self.button.setImage(UIImage(named: "namebutton"), for: .normal)

when you want to change image of button on swift 3 , you could try my solution, believe with me because i tried it and success . 当你想改变swift 3上按钮的图像时,你可以尝试我的解决方案,相信我,因为我尝试了它并取得了成功。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM