gpt4 book ai didi

ios - 背景图像未加载到按钮 ios 8 swift

转载 作者:行者123 更新时间:2023-11-29 01:51:27 24 4
gpt4 key购买 nike

您好,我在使用 UIImage(named: String) 将 Assets 目录中的图像加载到按钮背景时遇到问题按钮完全显示为白色,图像对象等于 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 的屏幕截图

Screenshot

最佳答案

只需从图像字符串中删除 .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")
}

输出是:

image is available



enter image description here

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

关于ios - 背景图像未加载到按钮 ios 8 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com