背景图像没有加载到按钮ios 8 swift上

时间:2015-07-12 10:22:57

标签: ios swift uiimage

您好我在使用UIImage(named: String)将资源目录中的图片加载到按钮的背景上时出现问题,该按钮只显示完全白色且图像对象等于nil

这是我到目前为止的代码

//
//  TopButtonContainer.swift
//  Risky-Buisiness-Museum-App-iOS
//
//  Created by THOMAS NEEDHAM on 11/07/2015.
//  Copyright (c) 2015 THOMAS NEEDHAM. All rights reserved.
//

import Foundation
import UIKit

public class TopButtonContainer : UIViewController {

var TopButtons: [UIButton] = [UIButton]();
let TopButtonImageNamesBlue = [ "Aquarium Icon Blue", "Bugs Icon Blue", "Ancient World Icon Blue", "World Cultures Icon Blue", "Dinosaurs Icon Blue", "Space Icon Blue"]

let TopButtonImageNamesGreen = [ "Aquarium Icon Green", "Bugs Icon Green", "Ancient World Icon Green", "World Cultures Icon Green", "Dinosaurs Icon Green", "Space Icon Green"]

var parent: HomeViewController?
let NUM_BUTTONS:CGFloat = 6

required public init(coder aDecoder: NSCoder) {
    parent = HomeViewController(coder: aDecoder)
    super.init(coder: aDecoder);
}

override public func viewDidLoad() {
    super.viewDidLoad()
    parent = getParent() as? HomeViewController;
    for var i:CGFloat = 0; i < NUM_BUTTONS; i++
    {
        self.view.addSubview(CreateButton(i))
    }

    //Cont.addSubview(CreateButton(CGFloat(1)))
    NSLog("Top buttons Loaded")
}

override public func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

private func CreateButton(index:CGFloat) -> UIButton{
    let Viewwidth: CGFloat = self.view.bounds.width
    let Viewheight: CGFloat = self.view.bounds.height
    var i:Int = Int(index)
    var value:String = TopButtonImageNamesBlue[i]
    var button = UIButton(frame: CGRect(x: Viewwidth / NUM_BUTTONS * index, y:CGFloat(0.0), width: Viewwidth / NUM_BUTTONS, height: Viewheight))
    var image:UIImage? = (UIImage(named: TopButtonImageNamesBlue[i])) // loading image here
    if(image == nil){
        NSLog("NIL")
    }
    button.setImage(image, forState: UIControlState.Normal)
    button.addTarget(self, action:"ButtonClicked:", forControlEvents: UIControlEvents.TouchDown)

    TopButtons.append(button);
    return button
}
func ButtonClicked(sender: UIButton){
    for var i:Int = 0; i < TopButtons.count; i++
    {
        if(TopButtons[i] == sender){
            NSLog("Button %i Was Pressed", i)

        }
    }

}

public func getParent() -> UIViewController?{
    return self.presentingViewController as? HomeViewController
}

internal func LoadMapButtons(){
    for var i = 0; i < self.TopButtons.count; i+=1 {
        sleep(1000)
    }
    // todo load map buttons

}

public func resetButtons(index:Int){
    for var i = 0; i < TopButtons.count; i+=1{
        if(i == index){
            continue
        }
        else{
            // todo reset button images
        }
    }
}
}

我已经被困在这几天了,并且已经尝试了有关stackoverflow的其他建议,但没有一个解决问题

提前致谢

编辑images.xcassets

的EDIT截图

Screenshot

1 个答案:

答案 0 :(得分:1)

只需从图片字符串中移除.png

即可
let TopButtonImageNamesBlue = [ "Aquarium Icon Blue", "Bugs Icon Blue", "Ancient World Icon Blue", "World Cultures Icon Blue", "Dinosaurs Icon Blue", "Space Icon Blue"]

这是我的工作代码:

    var images = ["01","02","03"]

    var image : UIImage? = UIImage(named: images[0])

    if (image == nil) {
        println("null")
    }
    else{

        var button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        button.frame = CGRectMake(0, 0, 50, 50)
        button.setImage(image, forState: .Normal)
        self.view.addSubview(button)

        println("image is available")
    }

输出是:

  

图片可用

enter image description here

您必须提供与assets文件夹相同的名称。