ios – 如何在SwiftXcode中为按钮设置图像

ios – 如何在SwiftXcode中为按钮设置图像,第1张

概述尝试以编程方式设置按钮图像时,我收到“nil”错误.我究竟做错了什么?这些都不是可选项. @IBOutlet weak var scrollView: UIScrollView!var imageButton: UIButton!var index: Int = 0var images: [UIImage] = [UIImage(named: "image1.png")!, UIIma 尝试以编程方式设置按钮的图像时,我收到“nil”错误.我究竟做错了什么?这些都不是可选项.
@IBOutlet weak var scrollVIEw: UIScrollVIEw!var imagebutton: UIbutton!var index: Int = 0var 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
解决方法 我想你是再次从Image创建Image.

就这样吧

imagebutton.setimage(images[index],forState:UIControlState.normal)

希望它会奏效.

或者你可以这样做:

var images = [String]?

在vIEwDIDLoad中

images = ["image1.png","image2.png"]let img : UIImage = UIImage(named: images[index])!imagebutton = UIbutton.buttonWithType(UIbuttonType.System) as UIbuttonbutton.frame = CGRectMake(100,100,100)//Set your own frameimagebutton.setimage(img,forState:UIControlState.normal)

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

总结

以上是内存溢出为你收集整理的ios – 如何在Swift / Xcode中为按钮设置图像全部内容,希望文章能够帮你解决ios – 如何在Swift / Xcode中为按钮设置图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存