2

I'm new to swift and I'm just trying to create a subclass of uibutton. Except that I have this weird blue rounded rect appearing when the button is selected as shown below. When all I want is a nice white border.

enter image description here

The code of my class :

import UIKit
import QuartzCore

@IBDesignable
class ColorButton: UIButton {
    //MARK: PROPERTIES
    @IBInspectable var stickerColor: UIColor = UIColor.whiteColor() {
        didSet {
            configure()
        }
    }

    override var selected: Bool {
        willSet(newValue) {
            super.selected = newValue;
            if selected {
                layer.borderWidth = 1.0
            } else {
                layer.borderWidth = 0.0
            }
        }
    }

    //MARK: Initializers
    override init(frame : CGRect) {
        super.init(frame : frame)
        setup()
        configure()
    }

    convenience init() {
        self.init(frame:CGRectZero)
        setup()
        configure()
    }

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


    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
        configure()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
        configure()
    }

    func setup() {
        //Border color
        layer.borderColor = UIColor.whiteColor().CGColor
        //Corner Radius
        setUpCornerRadius()
    }

    func configure() {
        backgroundColor = stickerColor
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        setUpCornerRadius()
    }

    func setUpCornerRadius() {
        layer.cornerRadius = CGRectGetWidth(bounds) * 0.205
    }
}
Ambroise Collon
  • 3,839
  • 3
  • 18
  • 37

1 Answers1

5

I have checked your code. and I got same issue.

but If you set button type as Custom then this problem will not occur enter image description here

Output :

enter image description here

Found something for buttonType :

You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:

Note that this way the buttonType isn't explicitly set to anything, which probably means that it's UIButtonTypeCustom. The Docs don't seem to actually specify that, but since that's the 0 value in the enum, that's likely what happens (and that seems to be the observable behavior as well)

Source : https://stackoverflow.com/a/10278515/3202193

Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136