Why doesn't the background colour of my Storyb

2020-03-26 07:20发布

问题:

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.

回答1:

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.



回答2:

Just try checking that you don't have a line saying 'self.view.backgroundColor = ...' inside of your .m file. i.e. 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];



回答3:

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.



回答4:

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)


回答5:

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