简体   繁体   English

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

[英]Why does my Swift button show a small blue square when tapped?

I created a custom button that is designable in storyboard, and i styled the button select state.我创建了一个可在 storyboard 中设计的自定义按钮,并设置了按钮 select state 的样式。 Now when i tap the button blue square appear above that.现在,当我点击按钮时,蓝色方块出现在其上方。 This is my code:这是我的代码:

Swift version: 4.0 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")
        }
    }
}

these are image of my problem in selected state : is unselect state这些是我在选定 state中的问题的图像:是取消选择 state

TylerTheCompiler wrote: TylerTheCompiler 写道:

If your button's type is "System" in your storyboard, try setting it to "Custom".如果您的 storyboard 中的按钮类型是“系统”,请尝试将其设置为“自定义”。

Then, original poster wrote:然后,原始海报写道:

so tnx comrade the problem is solved所以tnx同志问题解决了

(Just marking this as Answered, so it doesn't look unanswered in the future.) (只需将其标记为已回答,因此将来看起来不会未回答。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM