Ios 如何更改自定义UIButton的边框宽度
所以,我正在为我的应用程序编写简单的表单。我有两个按钮,允许用户选择是否要输入电子邮件或电话号码。我做了两个按钮,看起来像安卓风格的标签按钮。如下图所示: 所以我为我的按钮定制了类。代码如下:Ios 如何更改自定义UIButton的边框宽度,ios,uibutton,uikit,Ios,Uibutton,Uikit,所以,我正在为我的应用程序编写简单的表单。我有两个按钮,允许用户选择是否要输入电子邮件或电话号码。我做了两个按钮,看起来像安卓风格的标签按钮。如下图所示: 所以我为我的按钮定制了类。代码如下: @IBDesignable class HC24Button: UIButton { @IBInspectable var tabButtonBorderWidth: CGFloat = 1.0 @IBInspectable var borderWidth: CGFloat = 0{
@IBDesignable
class HC24Button: UIButton {
@IBInspectable var tabButtonBorderWidth: CGFloat = 1.0
@IBInspectable var borderWidth: CGFloat = 0{
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet{
updateView()
}
}
@IBInspectable var cornerRadius: CGFloat = 0{
didSet{
layer.cornerRadius = cornerRadius
}
}
@IBInspectable var tabsButton : Bool = false{
didSet{
updateView()
}
}
func updateView(){
let border = CALayer()
let width = tabButtonBorderWidth
border.borderColor = borderColor?.cgColor
border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width + 20, height: frame.size.height)
border.borderWidth = width
layer.addSublayer(border)
layer.masksToBounds = true
}
}
下面是我的视图控制器上的代码:
@IBAction func PhoneTapped(_ sender: HC24Button) {
sender.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
sender.tabButtonBorderWidth = 4.0
sender.borderColor = HC24AppColors.Main
EmailButton.tabButtonBorderWidth = 1.0
EmailButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
EmailButton.borderColor = .lightGray
UserPhoneEmailTextField.text = "62"
UserPhoneEmailTextField.keyboardType = .numberPad
}
@IBAction func EmailTapped(_ sender: HC24Button) {
sender.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
sender.tabButtonBorderWidth = 4.0
sender.borderColor = HC24AppColors.Main
PhoneButton.tabButtonBorderWidth = 1.0
PhoneButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
PhoneButton.borderColor = .lightGray
UserPhoneEmailTextField.text = ""
UserPhoneEmailTextField.placeholder = "Alamat Email"
}
但这是我得到的结果:
我只想要一个有彩色边框的按钮,比如开关。如何解决此问题?在调用按钮单击事件时,您不会更新视图,也不会删除先前添加的层,也不会更改该层的宽度
@IBDesignable
class HC24Button: UIButton {
@IBInspectable var tabButtonBorderWidth: CGFloat = 1.0 {
didSet {
updateView()
}
}
var borderLayer: CALayer?
@IBInspectable var borderWidth: CGFloat = 0{
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet{
updateView()
}
}
@IBInspectable var cornerRadius: CGFloat = 0{
didSet{
layer.cornerRadius = cornerRadius
}
}
@IBInspectable var tabsButton : Bool = false{
didSet{
updateView()
}
}
func updateView(){
let border = CALayer()
let width = tabButtonBorderWidth
border.borderColor = borderColor?.cgColor
border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width + 20, height: frame.size.height)
border.borderWidth = width
if let layerObj = self.borderLayer {
layerObj.removeFromSuperlayer()
}
self.borderLayer = border
layer.addSublayer(border)
layer.masksToBounds = true
}
}
在updateView()
中,您正在添加新边框,但从未真正删除取消选中选项卡的边框。与其每次都创建边框,不如只创建一次,然后简单地更改其颜色以隐藏它
修改后的版本为:
@可设计的
HC24类按钮:UIButton{
@IBInspectable var tabButtonBorderWidth: CGFloat = 1.0
@IBInspectable var borderWidth: CGFloat = 0{
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet{
updateView()
}
}
@IBInspectable var cornerRadius: CGFloat = 0{
didSet{
layer.cornerRadius = cornerRadius
}
}
@IBInspectable var tabsButton : Bool = false{
didSet{
updateView()
}
}
var border: CALayer?
func updateView(){
if let border = border {
border.borderColor = borderColor?.cgColor
border.borderWidth = tabButtonBorderWidth
return
}
border = CALayer()
border.borderColor = borderColor?.cgColor
let width = tabButtonBorderWidth
border.borderWidth = width
border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width + 20, height: frame.size.height)
layer.addSublayer(border)
layer.masksToBounds = true
}
}您可以创建如下自定义按钮:
class CustomButton: UIButton {
override func awakeFromNib() {
layer.borderWidth = 0.5
}
}
并更改awakeFromNib中的属性 有关示例/逻辑,请参见此
sender.tabButtonOrderWidth=4.0
,以便更改按钮底部栏。但你不能把另一个换成0。另外,一个强烈的建议是:以小写字母开始命名您的var。@Larme我确实更改了另一个。我更改了PhoneButton上的EmailButton边框,反之亦然。@EgaSetyaPutra只需将我的按钮类更改为您的按钮类,我已在下面回答了。你不会得到这个问题。根据你的答案宽度永远不会改变。回答之前先检查一下。谢谢,现在修好了。