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 trialMax Lettenberger
2,017 PointsTint color of button does not change to match background color.
In fact it doesn't change at all when the button's pressed. My code is identical to Pasan's.
The background color changes perfectly as it should.
Please let me know what my problem might be!
@IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
funFactButton.tintColor = randomColor
3 Answers
Nick Jones
2,086 PointsHi Max
I'm assuming you've already created an IBOutlet for the funFactButton what with the last line having the name of said button
funFactButton.tintColor = randomColor
If this is the case is there any chance you could post your whole viewController? Then we can definitely figure out for you what is going on :)
Max Lettenberger
2,017 PointsThanks for the answer! (funFactButton tintColor = randomColor did not work.)
import UIKit
class ViewController: UIViewController { @IBOutlet weak var funFactLabel: UILabel! @IBOutlet weak var funFactButton: UIView!
let factBook = FactBook()
let colorWheel = ColorWheel()
override func viewDidLoad() {
super.viewDidLoad()
funFactLabel.text = factBook.randomFact()
// Do any additional setup after loading the view, typically from a nib
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
funFactButton.tintColor = randomColor
funFactLabel.text = factBook.randomFact()
}
}
Nick Jones
2,086 PointsHi Max
Thanks for posting your code for me. The problem looks to be that you have:
UIViewController {
@IBOutlet weak var funFactLabel: UILabel!
@IBOutlet weak var funFactButton: UIView!
Instead of
class ViewController: UIViewController {
@IBOutlet weak var funFactLabel: UILabel!
@IBOutlet weak var funFactButton: UIButton!
The only difference between the two is, if you noticed, that funFactButton should be of type UIButton as opposed to UIView.
Interface Builder is a little fiddly so what I think might have happened is when you have correctly Option-Clicked on the button and then dragged it in to your code I believe it has actually pulled across your view instead.
Would you be able to try deleting:
@IBOutlet weak var funFactButton: UIView!
From your code and then going back in to interface builder. Once there double click on the edge of the button so that the rest of the view fades out slightly, this way you'll know that any actions you do will be related to the button rather than the view itself. Once you have the button selected simply do as you did before and Option-Click and drag in to your code in the same place as before. So long as you name the new IBOutlet the same as before and as long as it then shows up as UIButton such as:
@IBOutlet weak var funFactButton: UIView!
Then you should be good to go.
Any problems just let me know.
tytyty
4,974 PointsThanks Nick Yup I had the same issue! I wonder how that happened? Anyway I just reconnected it and bingo.
I guess it would not have worked if we just changed the name manually to UIButton!? It's code so why wouldn't it work?
Rogelio Valdez
6,244 PointsI have a similar problem where the color of the UIButton does not change. I already used a println(funFactButton.tintColor) and the color is changing but it is not displayed.
What I tried: I changed the color of the UIButton in the attributes inspector to default and now the color changes to match the background except at the beginning of the program since it shows as blue (default color). I think the Attribute Inspector is overriding the change color or maybe I changed and option in there that does not let me change the color unless I have it default.
The problem with this solution: The first fact that is displayed has a blue tint(default).
Anyone have an Idea of why this is happening?
Jonathan Meier
1,623 PointsI've got the same problem here.
valter Levander
345 PointsAdd this line of code "var tintColor: UIColor!"
Max Lettenberger
2,017 PointsMax Lettenberger
2,017 PointsFigured it out! Thanks!