0

I would like to change the background of my game view controller, However the classic self.view.backgroundColor = UIColor.red for example isn't working and I am not sure why. Could someone help me out? maybe I am missing something simple here is some code

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(red: 35/255.0, green: 35/255.0, blue: 33/255.0, alpha: 1.0)




    codedLabel.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
    codedLabel.textAlignment = .center
    codedLabel.text = "Box and Weave"
    codedLabel.numberOfLines=1
    codedLabel.textColor=UIColor.white
    codedLabel.font=UIFont.systemFont(ofSize: 25)
    codedLabel.backgroundColor = UIColor(red: 35/255.0, green: 35/255.0, blue: 33/255.0, alpha: 1.0)

    self.view.addSubview(codedLabel)
    codedLabel.translatesAutoresizingMaskIntoConstraints = false
    codedLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
    codedLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
    codedLabel.centerXAnchor.constraint(equalTo: codedLabel.superview!.centerXAnchor).isActive = true
    codedLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true




    playButton.translatesAutoresizingMaskIntoConstraints = false
    playButton.backgroundColor = UIColor.white
    playButton.setTitle("Play!", for: .normal)
    playButton.setTitleColor(UIColor.blue, for: .normal)
    playButton.titleLabel!.font =  UIFont(name: "HelveticaNeue-Thin", size: 23)
    playButton.titleLabel?.adjustsFontSizeToFitWidth = true
    playButton.titleLabel?.minimumScaleFactor = 0.5
    playButton.layer.cornerRadius = 25
    self.view.addSubview(playButton)
    // contraints for button
    let buttonHeight = playButton.heightAnchor.constraint(equalToConstant: 50)
    let buttonWidth = playButton.widthAnchor.constraint(equalToConstant: 125)
    let xPlacement = playButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
    let yPlacement = playButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)

    buttonConstraints = [buttonHeight, buttonWidth, xPlacement, yPlacement]
    NSLayoutConstraint.activate(buttonConstraints)
    playButton.addTarget(self, action: #selector(playButtonMethod), for: .touchUpInside)

}

This is how it looks when loaded What the UI tree looks like

lirikb
  • 81
  • 6
  • What color *is* it showing up as then? – Travis Griggs Jan 23 '19 at 17:38
  • @TravisGriggs I have added the photo, the background is a light gray but the rgb values I have provided are a dark gray – lirikb Jan 23 '19 at 17:47
  • Also, your label background color is black instead of the same anticipated dark gray color. – Travis Griggs Jan 23 '19 at 17:51
  • You might try running your app and using the `View UI Hierarchy` to examine your view tree. Might illuminate something? Need instructions on how to do that? – Travis Griggs Jan 23 '19 at 17:57
  • @TravisGriggs yes please, Also my label background color is actually the correct color and it is a very dark grey. I would like that color to be the background of the entire view controller – lirikb Jan 23 '19 at 18:36
  • OK, it looked black, I should have fired up my color sniffer and looked closer. You are correct. – Travis Griggs Jan 23 '19 at 18:37
  • To examine the view tree, run your app from Xcode (simulator or device, doesn't matter). In the left pane, you want to click the icon that has two horizontal lines on either side of 3 rows of horizontal dashes (3rd from right). Tooltip says 'Show the Debug navigator`. Then the circle with the three vertical lines in that pane, is actually a menu button. Click it and select `View UI Hierarchy`. There are a lot of ways you can see your view tree now. Symbolically on the left. But you can click on the view in Xcode as well and see attributes. You can even flip to 3D mode. – Travis Griggs Jan 23 '19 at 18:41
  • My hunch is that you've got an extra view layer in there that is occluding the self.view that you're referencing in your code. – Travis Griggs Jan 23 '19 at 18:42
  • @TravisGriggs could it possibly be the SKView that is messing up my code? I have attached a picture of the UI Tree – lirikb Jan 23 '19 at 18:53
  • Must be. See https://stackoverflow.com/questions/37635528/unable-to-change-skview-backgroundcolor – Travis Griggs Jan 23 '19 at 19:02

0 Answers0