Ios 如何在UIButton中创建边框?

Ios 如何在UIButton中创建边框?,ios,objective-c,Ios,Objective C,我在名为“addButton”的应用程序中使用自定义按钮,我想将其边框为白色。如何在自定义按钮周围获得白色边框?非常简单,只需在文件中添加quartzCore标题(为此,您必须将quartz框架添加到项目中) 然后再这样做 [[button layer] setCornerRadius:8.0f]; [[button layer] setMasksToBounds:YES]; [[button layer] setBorderWidth:1.0f]; 您可以根据需要更改浮点值 享受 下面

我在名为“addButton”的应用程序中使用自定义按钮,我想将其边框为白色。如何在自定义按钮周围获得白色边框?

非常简单,只需在文件中添加quartzCore标题(为此,您必须将quartz框架添加到项目中)

然后再这样做

[[button layer] setCornerRadius:8.0f];

[[button layer] setMasksToBounds:YES];

[[button layer] setBorderWidth:1.0f];
您可以根据需要更改浮点值

享受


下面是一些典型的现代代码

self.buttonTag.layer.borderWidth = 1.0f;
self.buttonCancel.layer.borderWidth = 1.0f;

self.buttonTag.layer.borderColor = [UIColor blueColor].CGColor;
self.buttonCancel.layer.borderColor = [UIColor blueColor].CGColor;

self.buttonTag.layer.cornerRadius = 4.0f;
self.buttonCancel.layer.cornerRadius = 4.0f;
这与分段控件的外观相似

Swift更新:

  • 无需添加“QuartzCore”
只要做:

button.layer.cornerRadius = 8.0
button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.black.cgColor

通过访问按钮的图层特性,可以在CALayer上设置边框特性

首先,加入石英

#import <QuartzCore/QuartzCore.h>
见:

上面链接中的CALayer允许您设置其他属性,如角半径、maskToBounds等

还有一篇关于按钮乐趣的好文章:


在swift中,您不需要导入“QuartzCore/QuartzCore.h”

只需使用:

button.layer.borderWidth = 0.8
button.layer.borderColor = (UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )).cgColor


现在不需要导入
QuartzCore.h
。参考iOS 8 sdk和Xcode 6.1

直接使用:

[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor greenColor].CGColor];

设置层的
borderWidth
borderColor
的问题在于,当您触摸按钮时,边框不会设置高光效果的动画

当然,您可以观察按钮的事件并相应地更改边框颜色,但这感觉是不必要的

另一个选项是创建可拉伸的UIImage并将其设置为按钮的背景图像。您可以在Images.xcsets中创建图像集,如下所示:

self.myBtn.layer.cornerRadius = 10;
self.myBtn.layer.borderWidth = 1;
self.myBtn.layer.borderColor =[UIColor colorWithRed:189.0/255.0f green:189.0/255.0f blue:189.0/255.0f alpha:1.0].CGColor;

然后,将其设置为按钮的背景图像:

如果您的图像是模板图像,则可以设置按钮的着色颜色,边框将更改:


现在,当按下按钮时,边框将与按钮的其余部分一起高亮显示。

要更改按钮半径、颜色和宽度,我设置如下:

self.myBtn.layer.cornerRadius = 10;
self.myBtn.layer.borderWidth = 1;
self.myBtn.layer.borderColor =[UIColor colorWithRed:189.0/255.0f green:189.0/255.0f blue:189.0/255.0f alpha:1.0].CGColor;

下面是一个
UIButton
子类,它支持高亮显示的状态动画,而不使用图像。它还会在视图的着色模式更改时更新边框颜色

类边框按钮:UIButton{
重写初始化(帧:CGRect){
super.init(frame:frame)
layer.borderColor=tintColor.cgColor
layer.borderWidth=1
层角半径=5
contentEdgeInsets=UIEdgeInsets(顶部:5,左侧:10,底部:5,右侧:10)
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“不支持NSCoding”)
}
重写func tintColorDidChange(){
super.tintColorDidChange()
layer.borderColor=tintColor.cgColor
}
覆盖变量isHighlighted:Bool{
迪塞特{
让fadedColor=tintColor.withAlphaComponent(0.2).cgColor
如果有灯光{
layer.borderColor=fadedColor
}否则{
layer.borderColor=tintColor.cgColor
让动画=动画化(关键路径:“borderColor”)
animation.fromValue=fadedColor
animation.toValue=tintColor.cgColor
动画。持续时间=0.4
添加(动画,forKey:nil)
}
}
}
}
用法:

let button=BorderedButton(style:.System)//style.System很重要

外观:

使用Swift 3更新

    button.layer.borderWidth = 0.8
    button.layer.borderColor = UIColor.blue.cgColor

以下是本·帕卡德的最新版本(Swift 3.0.1

由于使用了
@IBDesignable
@IBInspectable
标记,生成的按钮可以在故事板中使用

此外,还定义了两个属性,允许您直接在interface builder上设置边框宽度和颜色,并预览结果

可以以类似的方式添加其他属性,如边框半径和高光衰减时间。

****在Swift 3中****

创建边框

 btnName.layer.borderWidth = 1
 btnName.layer.borderColor = UIColor.black.cgColor
圆角

 btnName.layer.cornerRadius = 5

这可以通过2017年8月最新版本的Swift 3.0中的各种方法实现

选项1:

直接为UI按钮指定borderWidth属性值:

  btnUserButtonName.layer.borderWidth = 1.0
btnUserButtonName.setTitleColor(UIColor.darkGray, for: .normal)
btnUserButtonName.layer.borderColor = UIColor.red
   let myGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )
   btnUserButtonName.layer.borderColor = myGrayColor.cgColor
//use the method and call whever the border has to be applied

btnUserButtonName.setBordersSettings()
使用UI按钮的默认颜色值设置标题:

  btnUserButtonName.layer.borderWidth = 1.0
btnUserButtonName.setTitleColor(UIColor.darkGray, for: .normal)
btnUserButtonName.layer.borderColor = UIColor.red
   let myGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )
   btnUserButtonName.layer.borderColor = myGrayColor.cgColor
//use the method and call whever the border has to be applied

btnUserButtonName.setBordersSettings()
为UI按钮的边框属性值设置默认颜色的边框:

  btnUserButtonName.layer.borderWidth = 1.0
btnUserButtonName.setTitleColor(UIColor.darkGray, for: .normal)
btnUserButtonName.layer.borderColor = UIColor.red
   let myGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )
   btnUserButtonName.layer.borderColor = myGrayColor.cgColor
//use the method and call whever the border has to be applied

btnUserButtonName.setBordersSettings()
为UI按钮的边框属性值设置用户定义的颜色:

  btnUserButtonName.layer.borderWidth = 1.0
btnUserButtonName.setTitleColor(UIColor.darkGray, for: .normal)
btnUserButtonName.layer.borderColor = UIColor.red
   let myGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )
   btnUserButtonName.layer.borderColor = myGrayColor.cgColor
//use the method and call whever the border has to be applied

btnUserButtonName.setBordersSettings()
选项2:[推荐]

使用扩展方法,这样应用程序中的按钮看起来是一致的,不需要在每个地方重复多行代码

//Create an extension class in any of the swift file

extension UIButton {
func setBordersSettings() {
        let c1GreenColor = (UIColor(red: -0.108958, green: 0.714926, blue: 0.758113, alpha: 1.0))
        self.layer.borderWidth = 1.0
        self.layer.cornerRadius = 5.0
        self.layer.borderColor = c1GreenColor.cgColor
        self.setTitleColor(c1GreenColor, for: .normal)
        self.layer.masksToBounds = true
    }
}
代码中的用法:

  btnUserButtonName.layer.borderWidth = 1.0
btnUserButtonName.setTitleColor(UIColor.darkGray, for: .normal)
btnUserButtonName.layer.borderColor = UIColor.red
   let myGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )
   btnUserButtonName.layer.borderColor = myGrayColor.cgColor
//use the method and call whever the border has to be applied

btnUserButtonName.setBordersSettings()
扩展方法按钮的输出:

  btnUserButtonName.layer.borderWidth = 1.0
btnUserButtonName.setTitleColor(UIColor.darkGray, for: .normal)
btnUserButtonName.layer.borderColor = UIColor.red
   let myGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )
   btnUserButtonName.layer.borderColor = myGrayColor.cgColor
//use the method and call whever the border has to be applied

btnUserButtonName.setBordersSettings()
Swift 5

button.layer.borderWidth = 2
要更改边框的颜色,请使用

button.layer.borderColor = CGColor(srgbRed: 255/255, green: 126/255, blue: 121/255, alpha: 1)

确保在目标编辑器的“构建阶段”选项卡中将目标链接到QuartCore框架。是否需要导入quartz?我仍然可以访问没有它的按钮层。我想问的是:导入Quartzplus 1使用
CGColor
制作图像时,我们会得到什么好处?为了制作图像,我使用了Sketch,但你可以使用Gimp、Illustrator、Photoshop…回答得很好。这比更改层的
borderWidth
要好。我对swift 4的约束有问题,我做了全尺寸按钮(宽度从一边到另一边),在xcode中我看到了边框预览,但在模拟器或设备上,我看到边框缩小到了按钮内部文本的大小,我添加的唯一自动布局约束是水平居中,如何修复它?谢谢@克里斯蒂安·查帕罗。原因是,
prepareForInterfaceBuilder
仅从IB调用,而不是在运行应用程序时调用。因此,在
awakeFromNib
中也设置UIEDGEINSET,然后在运行应用程序时也会显示。根据XCode 9,“CGColor已重命名为CGColor”。b