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


William Wylie-Modro
William Wylie-Modro
8,031 Points

Button Borders Function Only Gives Back White Borders

I'm trying to create a Button Borders Function in Swift to help style my UI. However whatever RGB values I pass in/initialize the function only creates white borders.

Here is my function:

func buttonsWithBorders(button: UIButton, borderWidth: CGFloat, redcolour: CGFloat , greencolour: CGFloat, bluecolour: CGFloat, alpha: CGFloat?) {

    let redcolour : CGFloat = 7.0
    var greencolour : CGFloat = 3.0
    var bluecolour : CGFloat = 2.0
    var alpha: CGFloat = 1.0
    var widthOfBorder: CGFloat = borderWidth
    var theButtonWithBorders: UIButton

    var buttonBorderColour : UIColor = UIColor(red: redcolour, green: greencolour, blue: bluecolour, alpha: alpha)

    button.layer.borderWidth = widthOfBorder
    return button.layer.borderColor = buttonBorderColour.CGColor

And I call it using:

buttonsWithBorders(learnHomeButton, 2.0,2.0, 5.0, 5.0, 1.0)

Also I know that passing in values and initializing them is incorrect but Xcode complaines that I am not initializing before using them otherwise

Any help would be very much appreciated, Cheers

1 Answer

The issue here is that you're setting entirely new variables inside your function that have the same names as the parameters you're passing in. The compiler now thinks you want to use those rather than the parameters. Get rid of them and you'll get the results you're looking for. Also get rid of the return statement since you aren't returning anything in this function. You should be good to go now!

 func buttonsWithBorders(button: UIButton, borderWidth: CGFloat, redColour: CGFloat , greenColour: CGFloat, blueColour: CGFloat, alpha: CGFloat?) {

var buttonBorderColour : UIColor = UIColor(red: redColour, green: greenColour, blue: blueColour, alpha: alpha)

button.layer.borderWidth = borderWidth 
button.layer.borderColor = buttonBorderColour.CGColor
William Wylie-Modro
William Wylie-Modro
8,031 Points

Well that makes me feel stupid. Thank you very much! And Happy Holidays!