ios - 为什么我的 Swift 按钮在点击时会显示一个蓝色的小方 block ?

标签 ios swift uibutton

我创建了一个可在 Storyboard中设计的自定义按钮,并设置了按钮选择状态的样式。现在,当我点击按钮时,上面会出现蓝色方 block 。这是我的代码:

swift 版本:4.0

@IBDesignable class CircularButton: UIButton {

    @IBInspectable var borderColor: UIColor = UIColor(red: 255.0, green: 153.0, blue: 0.0, alpha: 1.0)

    var borderWidth: CGFloat = 3.0

    @IBInspectable var bgColor: UIColor? {
        didSet {
            updateButton()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpButton()
        self.addTarget(self, action: #selector(btnTapp), for: .touchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func setUpButton() {
        self.backgroundColor = self.bgColor
        let  cornerRadius = (self.frame.size.width + self.frame.size.height) / 4
        self.layer.cornerRadius = cornerRadius
    }

    @objc func btnTapp(_ sender: UIButton) {
        if isSelected == false {
            isSelected = !isSelected
        } else if isSelected == true {
            isSelected = !isSelected
        }
    }

    override var isSelected: Bool {
        didSet {
            updateState(state: isSelected)
        }
    }

    func updateState(state: Bool) {
        if state == false {
            self.layer.borderWidth = 0.0
            self.layer.borderColor = borderColor.cgColor
            print("fls")
        } else if state == true {
            self.layer.borderWidth = 3.0
            self.layer.borderColor = borderColor.cgColor
            print("tre")
        }
    }
}

这些是我的问题的图片in selected state : is unselect state

最佳答案

TylerTheCompiler 写道:

如果您的按钮类型在 Storyboard中是“系统”,请尝试将其设置为“自定义”。

然后,楼主写道:

所以tnx同志问题解决了

(只是将其标记为已回答,这样以后看起来就不会没有回答了。)

关于ios - 为什么我的 Swift 按钮在点击时会显示一个蓝色的小方 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58570994/

相关文章:

xcode - 由于内存错误而终止的应用程序

iphone - 如何从我的应用程序启动 iPhone 设置应用程序?

ios - Jenkins iOS 归档错误与 Pods FIRCoreDiagnostics.m 正常armv7

ios - 实现 searchBarSearchButtonClicked

ios - UINib(iOS): possible to instantiate more than one view from a nib file?

ios - UICollectionView 显示隐藏动画问题

ios - 如何动态指示 SWIFT 函数中按钮的属性

ios - UIView 处理手势,内部 UIButton 应该捕捉触摸

ios - 我如何为 UIButton iOS 创建脉动动画?

iphone - 我可以在 UISearchDisplayController 中插入一个按钮吗?