gpt4 book ai didi

ios - Swift:UIButton 自定义

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:12 26 4
gpt4 key购买 nike

作为最佳实践,我想将代码从我的 viewcontroller 类中移出以自定义 UIButtons。我下面的代码是向 UIButtons 添加一个白色边框,我想在整个项目的按钮上轻松调用它。

    //White Border
let passwordBorder = CALayer()

let width = CGFloat(5.0)

passwordBorder.borderColor = UIColor.whiteColor().CGColor

passwordBorder.frame = CGRect(x: 0, y: 0, width: passwordField.frame.size.width, height: passwordField.frame.size.height)

passwordBorder.borderWidth = width

passwordField.layer.addSublayer(passwordBorder)

passwordField.layer.masksToBounds = true

如何将此代码放入辅助函数中以便我可以轻松地调用它?

我是编码新手,在任何UI 上使用辅助函数时遇到问题。谢谢!

最佳答案

看看 Swift 的扩展。你可以很容易地做类似的事情

extension UIButton {
func setPasswordBorderColor(borderColor: UIColor) {
//White Border
let passwordBorder = CALayer()

let width = CGFloat(5.0)

passwordBorder.borderColor = borderColor.CGColor

passwordBorder.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)

passwordBorder.borderWidth = width

self.layer.addSublayer(passwordBorder)

self.layer.masksToBounds = true
}
}

关于ios - Swift:UIButton 自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37536881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com