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
Alain Ghiai
4,287 PointsHaving trouble with swift.
I created an app like the simple app course (facts) and I want to add some more features. I want that every time that I tap the button for a new fact it changes the background color.I've done that. However I have a UiTextView and a UILabel that won't get the same background color. i've used this :
func randomColor() -> UIColor{
var randomRed:CGFloat = CGFloat(drand48())
var randomGreen:CGFloat = CGFloat(drand48())
var randomBlue:CGFloat = CGFloat(drand48())
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}
view.backgroundColor = randomColor()
2 Answers
Steve Hunter
57,712 PointsHi Alain,
Do you want the TextView and the Label to have the same colour? I assume you want the background of the TextView and the font color of the label to be the same? Here's some code that will save one colour in a variable as it is returned from your function. That same colour is then applied to a couple of features on the screen. Is that what you're trying to achieve? It doesn't have a label in there, but I'm sure you can easily adapt that?
Steve.
let randomColor = randomColor() // call the function; store the result
view.backgroundColor = randomColor // apply the result
funFactButton.tintColor = randomColor // apply it again; same colour
Alain Ghiai
4,287 PointsThank you very much Steve but I figured it out, thanks again for your time and help!
Steve Hunter
57,712 PointsNo problem. Shout if we can help with anything else.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsSorry - that code was just copied out of the FunFacts app I had in Xcode.
What is it you are trying to do with your code? You have a UITextView and a UILabel. Your TextView seems to be called
view, that's fine. What's the UILabel called and which property do you want to change the colour of?I can then sugest code that would work for you.
Steve.