简体   繁体   中英

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

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

Thanks in advance

EDIT screenshot of images.xcassets

屏幕截图

Just remove .png from the image strings

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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