Ios Swift:ui按钮定制
作为最佳实践,我想将代码从我的Ios Swift:ui按钮定制,ios,xcode,swift,function,uibutton,Ios,Xcode,Swift,Function,Uibutton,作为最佳实践,我想将代码从我的viewcontroller类中移出,以自定义ui按钮。我下面的代码是为UIButtons添加一个白色边框,我想在整个项目中轻松地在按钮上调用它 //White Border let passwordBorder = CALayer() let width = CGFloat(5.0) passwordBorder.borderColor = UIColor.whiteColor().CGColor passwordBo
viewcontroller
类中移出,以自定义ui按钮。我下面的代码是为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
}
}
看看斯威夫特的扩展。你可以很容易地做类似的事情
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
}
}
没有代码,您可以配置按钮
选择您的按钮,然后选择Identity Inspector,然后添加
“用户定义的运行时属性”。有关详细信息,请参阅屏幕截图
在项目中添加CALayer+XibConfiguration.h&CALayer+XibConfiguration.m
对于CALayer+Xib配置,打开并下载CALayer+Xib配置
无需代码即可配置按钮
选择您的按钮,然后选择Identity Inspector,然后添加
“用户定义的运行时属性”。有关详细信息,请参阅屏幕截图
在项目中添加CALayer+XibConfiguration.h&CALayer+XibConfiguration.m
对于CALayer+Xib配置,打开并下载CALayer+Xib配置
有几种方法可以实现这一点,比如使用category、子类化UIButton
,或者在类中创建函数(可能是所有其他类继承的基类),等等
通过使用函数,您可以
func cutomizeButton(frame : CGRect, title : String) -> UIButton {
let button : UIButton = UIButton(type: .Custom)
button.frame = frame
button.layer.borderWidth = 5.0
button.layer.borderColor = UIColor.whiteColor().CGColor;
button.layer.masksToBounds = true
button.titleLabel?.text = title;
//do other stuff
return button;
}
有几种方法可以实现这一点,比如使用category、子类化UIButton
,或者在类中创建函数(可能是所有其他类继承的基类),等等
通过使用函数,您可以
func cutomizeButton(frame : CGRect, title : String) -> UIButton {
let button : UIButton = UIButton(type: .Custom)
button.frame = frame
button.layer.borderWidth = 5.0
button.layer.borderColor = UIColor.whiteColor().CGColor;
button.layer.masksToBounds = true
button.titleLabel?.text = title;
//do other stuff
return button;
}
在helper类中,使方法类似
func customiseButton(button:UIButton){
//White Border
let passwordBorder = CALayer()
let width = CGFloat(5.0)
passwordBorder.borderColor = UIColor.whiteColor().CGColor
passwordBorder.frame = CGRect(x: 0, y: 0, width: button.frame.size.width, height: button.frame.size.height)
passwordBorder.borderWidth = width
button.layer.addSublayer(passwordBorder)
button.layer.masksToBounds = true
}
在ViewController中,将此方法称为
HelperClass().customiseButton(passwordField)
在helper类中,使方法类似
func customiseButton(button:UIButton){
//White Border
let passwordBorder = CALayer()
let width = CGFloat(5.0)
passwordBorder.borderColor = UIColor.whiteColor().CGColor
passwordBorder.frame = CGRect(x: 0, y: 0, width: button.frame.size.width, height: button.frame.size.height)
passwordBorder.borderWidth = width
button.layer.addSublayer(passwordBorder)
button.layer.masksToBounds = true
}
在ViewController中,将此方法称为
HelperClass().customiseButton(passwordField)
您可以尝试使用此圆形按钮扩展:
extension UIButton{
func roundCorners(corners:UIRectCorner, radius: CGFloat){
let borderLayer = CAShapeLayer()
borderLayer.frame = self.layer.bounds
borderLayer.strokeColor = UIColor.green.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.lineWidth = 10.5
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
borderLayer.path = path.cgPath
self.layer.addSublayer(borderLayer)
}
}
您可以尝试使用此圆形按钮扩展:
extension UIButton{
func roundCorners(corners:UIRectCorner, radius: CGFloat){
let borderLayer = CAShapeLayer()
borderLayer.frame = self.layer.bounds
borderLayer.strokeColor = UIColor.green.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.lineWidth = 10.5
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
borderLayer.path = path.cgPath
self.layer.addSublayer(borderLayer)
}
}
您希望按钮的自定义类型创建一个函数,该函数返回带有边框的按钮,就像您希望重用代码一样。您可以查看以下答案:您希望按钮的自定义类型创建一个函数,该函数返回带有边框的按钮,就像您希望重用代码一样。您可以查看以下答案: