user2176152
user2176152

Reputation: 67

UIButton 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

Upvotes: 2

Views: 471

Answers (3)

zisoft
zisoft

Reputation: 23078

The colour values need to be between 0.0 and 1.0 so you should define them as:

let redcolour : CGFloat = 7.0 / 255.0
var greencolour : CGFloat = 3.0 / 255.0
var bluecolour : CGFloat = 2.0 / 255.0

Upvotes: 0

Gregory Higley
Gregory Higley

Reputation: 16598

You aren't initializing them. You're declaring entirely new variables with the same names as the parameters you're passing in. Whenever you use let or var you are introducing a brand new variable.

When a new variable is introduced with the same name as another currently in scope, this is known as variable shadowing, and what you have here is an almost textbook case.

A better, more concise implementation of your function might look like this:

func addButtonBorder(button: UIButton, width: CGFloat, red: CGFloat, blue: CGFloat, green: CGFloat, alpha: CGFloat = 1.0) {
    button.layer.borderColor = UIColor(red: red, green: green, blue: blue, alpha: alpha).CGColor
    button.layer.borderWidth = width
}

I used a different name because buttonsWithBorders implies that one or more buttons will be returned from this function. That does not appear to be your intent. Since you are passing one button in, you could only ever get one out, but "buttons" implies more than one.

If I were going to initialize a lot of buttons with borders, I might do something like this:

extension UIButton {
    convenience init(frame: CGRect, borderColor: UIColor, borderWidth: CGFloat = 1.0) {
        self.init(frame: frame)
        setBorder(borderColor, borderWidth: borderWidth)
    }
    func setBorder(borderColor: UIColor, borderWidth: CGFloat = 1.0) {
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.CGColor
    }
}

Then you could say UIButton(frame: frame, borderColor: borderColor, borderWidth: 2.0) to initialize a new button or button.setBorder(borderColor, borderWidth: 2.0) to set the border on an existing button.

Upvotes: 1

Mehdi.Sqalli
Mehdi.Sqalli

Reputation: 510

UIColor takes a float between 0 and 1. So you want to divide your RGB Values by 255.0

Here is the code I used, that works on playground :

import Foundation

import UIKit

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

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

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

}

let learnHomeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))

buttonsWithBorders(learnHomeButton, 2.0, 177/255.0, 177/255.0, 177/255.0, 1.0)

I edited the code so you can pass the colors to the function as parameters. Hope it helps.

Upvotes: 0

Related Questions