简体   繁体   中英

Why doesn't the background colour of my Storyboard in Xcode change? It's always the same grey color?

So i've changed the background property of my default view in my storyboard file to as many as the colours in the rainbow. The colour shows up along with my buttons nicely in Xcode, but when I actually RUN the app, the background is always the same, ugly, default grey colour that you start with a new Xcode project ("Hello World!").

I've searched up for the answer all over Google and to no avail. Am I doing something wrong or is it just a glitch? It might be also important to mention than i'm using Spritekit, and that this "view" is the only view in my Storyboard file, the default one. Should I change self.view to skView? Would that work?

Oh and also, when I manually try to change the background colour under viewDidLoad in my .m class by doing:

self.view.backgroundColor = [UIColor backgroundColor]; 

the background STILL doesn't change and is always the same default colour. Please help me... i'm desperate right now... there's no answers anywhere to be found. Thanks.

All Storyboard, all graphical answer:

故事板

Explanation: You should modify the Main.storyboard , select the View Controller , change View in the Attribute Inspector , and apply a new Background color.

With exactly 0 lines of code, you should get this result:

在此处输入图像描述

Download project here .

I found myself in the same exact problem. After finding no help whatsoever online and exhausting myself trying different things; I decided to simply add a view to the storyboard and make it the background. This way you can change the background color and/or image and it will show up. It should be entirely constrained to the superview. I used the following constraints:

约束

If you need to add other things to the storyboard (which it wouldn't really make any sense if you didn't) every other object should be contained in this view (it basically replaces the superview). This works well for me and I hope it can help someone else too.

Just try checking that you don't have a line saying 'self.view.backgroundColor = ...' inside of your .m file. ie somewhere in 'loadView:', 'viewDidLoad:' or 'viewWillAppear:'

(I first thought that you have linked to some different viewController inside of your storyboard, but if you have all your buttons displaying correctly, then this assumption is wrong)

If you just want a quick fix of the problem, then simply add a line in your .m file saying self.view.backgroundColor = [UIColor yellowColor];

The UIView . backgroundColor property is inherited by SKView , but is visible only if the SpriteKit view isn't rendering any SpriteKit scene.

Of course, in practice a SpriteKit view just about always has an SKScene assigned to it. When the view is rendering a scene, it paints itself with the backgroundColor of whatever scene it's assigned to render.

(This means that if you present a different scene, the background will change. It also means that if your scene's scaleMode and size are such that the scene doesn't fill the entire view, the view's backgroundColor will be visible in areas not covered by the scene.)


You can set an SKScene 's background color in code using its backgroundColor property. If you create your scene graphically in an .sks file, you can set it in the inspector pane in Xcode:

具有颜色控制的场景编辑器检查器

IMPORTANT: This is the SpriteKit Scene inspector , seen when you edit a .sks file in Xcode -- not the view / view controller inspector that you see when editing a storyboard.

If you have the same problem as me! That you see the ugly gray background right after the SplashScreen, I have found a solution! Just after creating the skView, create an empty scene with a white background and present it directly!

let backgroundColoredScene = SKScene()    
backgroundColoredScene.backgroundColor = SKColor.white()    
skView.presentScene(backgroundColoredScene)

I had the same problem. Try to change the background color at main.storyboard as above, then restart Xcode.

The issue is with the SKView. A workaround I use is to change the SKView on the storyboard to a UIView. Then transition to an ViewControler with an SKView when you want to run your spritekit game. Here's where you can change the view type on the storyboard

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