ios 如何在 Swift/Xcode 中为按钮设置图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31488117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:55:24  来源:igfitidea点击:

How to set image for button in Swift/Xcode

iosxcodeswiftxcode7-beta3

提问by user190494

I am receiving a "nil" error when trying to set the image for a button programmatically. What am I doing wrong? These are all not optionals.

尝试以编程方式设置按钮的图像时收到“nil”错误。我究竟做错了什么?这些都不是可选的。

@IBOutlet weak var scrollView: UIScrollView!

var imageButton: UIButton!
var index: Int = 0

var images: [UIImage] = [UIImage(named: "image1.png")!, UIImage(named: "image2.png")!, UIImage(named: "image3.png")!, UIImage(named: "image4.png")!, UIImage(named: "image5.png")!, UIImage(named: "image6.png")!, UIImage(named: "image7.png")!, UIImage(named: "image8.png")!, UIImage(named: "image9.png")!, UIImage(named: "image10.png")!, UIImage(named: "image11.png")!, UIImage(named: "image12.png")!, UIImage(named: "image13.png")!, UIImage(named: "image14.png")!, UIImage(named: "image15.png")!, UIImage(named: "image16.png")!, UIImage(named: "image17.png")!, UIImage(named: "image18.png")!, UIImage(named: "image19.png")!, UIImage(named: "image20.png")!, UIImage(named: "image21.png")!, UIImage(named: "image22.png")!, UIImage(named: "image23.png")!, UIImage(named: "image24.png")!, UIImage(named: "image25.png")!, UIImage(named: "image26.png")!, UIImage(named: "image27.png")!, UIImage(named: "image28.png")!, UIImage(named: "image29.png")!, UIImage(named: "image30.png")!]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width
    //let screenHeight = screenSize.height

    let numWidth: CGFloat = 3
    let numHeight: CGFloat = 10
    self.scrollView.frame.size.width = screenWidth
    let width: CGFloat = (self.scrollView.frame.size.width - (numWidth + 1))/3

    for var i:CGFloat = 0; i < 3; ++i{
        for var j:CGFloat = 0; j < 10; ++j {

            let image: UIImage = images[index] as UIImage!


            imageButton.setImage(image,forState:UIControlState.Normal)
            imageButton.frame = CGRectMake(width*i, width*j, width, width)
            self.scrollView.addSubview(imageButton)

            index++
        }
    }
    scrollView.contentSize.height = (width)*numHeight

回答by Ashish Kakkad

I think you are creating Image from Image again.

我想你又在从 Image 创建 Image 了。

Just do it like

就这样吧

imageButton.setImage(images[index],forState:UIControlState.Normal)

Hope it will work.

希望它会起作用。

Or you can do it like :

或者你可以这样做:

var images = [String]?

in viewDidLoad

viewDidLoad

images = ["image1.png","image2.png"]
let img : UIImage = UIImage(named: images[index])!
imageButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)//Set your own frame
imageButton.setImage(img,forState:UIControlState.Normal)

And check for the image it must be available in your project

并检查它必须在您的项目中可用的图像

回答by user462990

The image needs to be in Assets.xcassets as an image set...works v.smooth!

图像需要在 Assets.xcassets 中作为图像集...作品 v.smooth!