简体   繁体   English

背景图像未加载到按钮iOS 8 Swift

[英]Background image not loading onto button ios 8 swift

Hi i am having a problem loading a image from the asset catalog onto the background of a button using UIImage(named: String) the button just shows up completely white and the image object is equal to nil 嗨,我在使用UIImage(named: String)将资产目录中的图像加载到按钮的背景时遇到问题,该按钮仅显示为完全白色,并且图像对象等于nil

here is the code i have so far 这是我到目前为止的代码

//
//  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
        }
    }
}
}

i have been stuck on this for days now and have tried other suggestions on stackoverflow but none of them fixed the problem 我已经在这个问题上停留了几天,并尝试了关于stackoverflow的其他建议,但是都没有解决问题

Thanks in advance 提前致谢

EDIT screenshot of images.xcassets 编辑images.xcassets的屏幕截图

屏幕截图

Just remove .png from the image strings 只需从图像字符串中删除.png

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

Here is my working code : 这是我的工作代码:

    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")
    }

Output is : 输出为:

image is available 图片可用

在此处输入图片说明

You must give the name same as assets folder. 您必须提供与资产文件夹相同的名称。

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

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