UIButton边框功能仅返回白色边框 [英] UIButton Borders Function Only Gives Back White Borders

查看:130
本文介绍了UIButton边框功能仅返回白色边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swift中创建一个Button Borders函数,以帮助设置UI样式.但是,无论我传入/初始化该函数的RGB值如何,都只会创建白色边框.

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.

这是我的功能:

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
}

我用它来称呼它

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

我也知道传入值并对其进行初始化是不正确的,但Xcode抱怨我在使用它们之前未进行初始化

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

推荐答案

您没有初始化它们.您将完全声明与传入参数同名的 new 变量.每当使用letvar时,您都会引入一个全新的变量.

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
}

我使用了另一个名称,因为buttonsWithBorders表示将从该函数返回一个或多个按钮.这似乎不是您的意图.由于您要传递一个按钮,因此您只能获得一个按钮,但是按钮"意味着不止一个按钮.

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
    }
}

然后,您可以说UIButton(frame: frame, borderColor: borderColor, borderWidth: 2.0)来初始化一个新按钮,或者说button.setBorder(borderColor, borderWidth: 2.0)来在现有按钮上设置边框.

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.

这篇关于UIButton边框功能仅返回白色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆