Can I set the color of the safe areas programmatically?

In GameViewController I do set the safeAreaLayouts. My only problem is that the top and bottom safe areas (iPhone X) are white.

Now I can change them via Main.storyboard. However, am working (or learning) my way to create apps without any storyboards.

Besides, perhaps I may to vary the background colors of the safeAreas.

So was looking for a way to do it with coding. Since I am hear, is there a way to also change the color of the text in the status bar as well?

Am pasting the complete code for GVC, just in case that has something to do with my question.

Code Block
import UIKit
import SpriteKit
class GameViewController: UIViewController {
public let myView : SKView = {
let myView = SKView()
myView.translatesAutoresizingMaskIntoConstraints = false
return myView
}()
private func addConstraints(){
var constraints = [NSLayoutConstraint]()
//add
constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor))
constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor))
constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor))
constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor))
//activate
NSLayoutConstraint.activate(constraints)
}
override func viewDidLoad() {
super.viewDidLoad()
#if DEBUG
print ("GVC viewDidLoad")
#endif
// if let view = self.view as! SKView?
view.addSubview(myView)
addConstraints()
var scene : GameScene!
// var scene = GameScene(size: myView.frame.size)
// let scene = GameScene(size: myView.frame.size)
DispatchQueue.main.async { [self] in scene = GameScene(size: myView.frame.size )
scene.anchorPoint = CGPoint(x: 0.0, y: 0.0)
scene.backgroundColor = .clear
scene.scaleMode = .aspectFit
myView.isHidden = false
myView.ignoresSiblingOrder = true
myView.showsFPS = true
myView.showsNodeCount = true
myView.showsPhysics = true
myView.presentScene(scene)
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
#if DEBUG
print ("GVC viewDidLayoutSubviews")
#endif
getScreenDimensions (screen: &screenDims)
if myGlobalVars.safeSceneRect == .zero {
myGlobalVars.sceneRect = view.frame
myGlobalVars.sceneRect = myView.frame
}
if #available(iOS 11.0, *) {
myGlobalVars.topSafeArea = view.safeAreaInsets.top
myGlobalVars.bottomSafeArea = view.safeAreaInsets.bottom
} else {
myGlobalVars.topSafeArea = topLayoutGuide.length
myGlobalVars.bottomSafeArea = bottomLayoutGuide.length
}
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .portraitUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return false
}
}

Answered by OOPer in 644605022
First of all, SwiftUI is not an appropriate tag for this thread.


My only problem is that the top and bottom safe areas (iPhone X) are white.

Your code has another problem which is continuing discussed in another thread of yours.

Safe areas are showing the main view of GameViewController, you just need to change the background color of it.
Code Block
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
//...
}



am working (or learning) my way to create apps without any storyboards.

You can learn how to build apps without using Main.storyboard, if you prefer.

Accepted Answer
First of all, SwiftUI is not an appropriate tag for this thread.


My only problem is that the top and bottom safe areas (iPhone X) are white.

Your code has another problem which is continuing discussed in another thread of yours.

Safe areas are showing the main view of GameViewController, you just need to change the background color of it.
Code Block
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
//...
}



am working (or learning) my way to create apps without any storyboards.

You can learn how to build apps without using Main.storyboard, if you prefer.

Can I set the color of the safe areas programmatically?
 
 
Q