gpt4 book ai didi

ios - 添加背景图片错误

转载 作者:行者123 更新时间:2023-11-28 08:43:18 25 4
gpt4 key购买 nike

我有以下代码行:

scene?.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)

当我运行它时,我看不到我的背景图像。但我只看到黑屏,上面有我制作的所有其他按钮和内容,但看不到背景图像。

很抱歉,您最后的建议也没有奏效。但我发现了这段代码:

let backgroundImage = UIImageView(frame: UIScreen.mainScreen().bounds)
backgroundImage.image = UIImage(named: "background.jpg")
self.view.insertSubview(backgroundImage, atIndex: 0)

这显示了我的背景图片!但是现在我再也看不到我的 SKSpriteNode 图像了...也许您对如何解决这个问题有什么建议?

最佳答案

您提供的代码是可以的。如果图像定位正确,问题必须是场景?变量。

在您的 viewDidLoad 函数中试试这个。

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
}

如果这不起作用,请尝试链接 Storyboard中的图像。添加 UIImageView 并在属性检查器中为 Image 属性设置背景图像。

Storyboard Image

我试过:

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Background")!)

在我的 Assets 中:

Assets

我得到了这个结果:

enter image description here

我在插入 subview 方法的苹果文档中找到了这个

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

这可能是你看不到它的原因。

关于ios - 添加背景图片错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940585/

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