Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App with Swift 2.0 Improving Our User Interface Changing the Background Color

Ramiro Martinez
Ramiro Martinez
13,858 Points

Changing view's background color Task 2

I got another error, I can't set the backgroung color here, but in Xcode works fine, here I'm just setting the instance to the background of the view

colors.swift
class ViewController: UIViewController {

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        super.viewDidLoad()
        let blueColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 255/255.0, alpha: 1.0)
        view.backgroundColor = blueColor
    }

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated
    }
}

3 Answers

Raffi Bagdasarian
Raffi Bagdasarian
3,147 Points

I was able to get the code to work using the convenience method instead of by specifying the RBGA values:

        let blueColor = UIColor.blueColor()

Your original code - with the stored value declared inside viewDidLoad() - should have worked as you had written it. As such, I suspect it's a bug in the way the RGBA method was implemented for this challenge.

First, blueColor is supposed to be a stored property and should probably be declared before overriding viewDidLoad method, not inside it. Second, in this challenge you don't need to use super keyword (I wish it was mentioned in the description though, very confusing considering the errors I was getting in the first task before realizing that there was a typo in class declaration). So inside overriding viewDidLoad method just assign blueColor to view.backgroundColor property as you already did and it should work fine.

It's a shame that Treehouse doesn't test its own courses with mistakes as obvious as having an incorrect name of the class and having a quote mark inside a string that somehow made everything I typed in interpreted as a comment.

Ramiro Martinez
Ramiro Martinez
13,858 Points

I declared the stored property before viewDidLoad, and now said that task one is no longer passing. Did you get pass this challenge?

class ViewController: UIViewController {

    let blueColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 255/255.0, alpha: 1.0)

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.

        view.backgroundColor = blueColor
    }

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated
    }
}