ios – Styling Customized UIButtons in Swift-StoryBoard — IBDesignables doesn’t work?

[ad_1]

I would like three buttons to be positioned in a row, the place the chosen button will get highlighted with a teal background and white textual content colour and different buttons have a white background, teal border and textual content colour.I’ve made a customized UIButton class for a similar like this;

@IBDesignable
class UICustomButton: UIButton {
    
    // MARK: - Initialization
      override init(body: CGRect) {
          tremendous.init(body: body)
          setupView()
      }

      required init?(coder: NSCoder) {
          tremendous.init(coder: coder)
      }
    
    // MARK: - UI Setup
      override func prepareForInterfaceBuilder() {
          setupView()
      }

      func setupView() {
          self.backgroundColor = colour
          self.layer.cornerRadius = 5
          self.layer.shadowColor = shadowColor.cgColor
          self.layer.shadowRadius = shadowRadius
          self.layer.shadowOpacity = shadowOpacity
          self.layer.borderWidth = 3
          self.layer.borderColor = UIColor.systemTeal.cgColor
      }
    
    func selectedButton() {
        self.backgroundColor = UIColor.systemTeal
        self.layer.cornerRadius = 5
        self.layer.borderWidth = 3
        self.layer.borderColor = UIColor.systemTeal.cgColor
        self.tintColor = UIColor.systemTeal
        self.setTitleColor(UIColor.systemTeal, for: .reserved)
        
    }
    
    func deselectedButton() {
        self.backgroundColor = UIColor.white
        self.layer.cornerRadius = 5
        self.layer.borderWidth = 3
        self.layer.borderColor = UIColor.systemTeal.cgColor
        self.setTitleColor(UIColor.systemYellow, for: .regular)
    }
    
    // MARK: - Properties
    @IBInspectable var colour: UIColor = .white {
          didSet {
              self.backgroundColor = colour
          }
      }
    
   @IBInspectable var cornerRadius: CGFloat = 5 {
        didSet {
            self.layer.cornerRadius = cornerRadius
        }
    }
    
    @IBInspectable var borderWidth: CGFloat = 3 {
        didSet {
            self.layer.borderWidth = borderWidth
        }
    }
    
    @IBInspectable var borderColor: UIColor = UIColor.systemTeal {
        didSet {
            self.layer.borderColor = borderColor.cgColor
        }
    }
    
    @IBInspectable var shadowColor: UIColor = UIColor.black {
        didSet {
            self.layer.shadowColor = shadowColor.cgColor
        }
    }
    
    @IBInspectable var shadowOffset: CGSize = CGSize(width: 0, top: 0) {
        didSet {
            self.layer.shadowOffset = shadowOffset
        }
    }
    
    @IBInspectable var shadowOpacity: Float = 0 {
        didSet {
            self.layer.shadowOpacity = shadowOpacity
        }
    }
    
    @IBInspectable var shadowRadius: CGFloat = 0 {
        didSet {
            self.layer.shadowRadius = shadowRadius
        }
    }
    
}

I’ve a standard clickhandler operate for the three buttons, which calls the deselectedButton() on all three buttons after which calls selectedButton() on the clicked button by checking its tag.

In my mission I face these points:

  1. Not one of the default values given to the @IBInspectable attributes present up, so I’ve to manually set them through a category operate.
  2. the prepareForInterfaceBuilder() doesn’t work, so in my ViewDidLoad(), I name the required operate for every button i.e. selectedButton() or deselectedButton() (initially I would like button 2 to be highlighted)
func selectedButton() {
        self.backgroundColor = UIColor.systemTeal
        self.layer.cornerRadius = 5
        self.layer.borderWidth = 3
        self.layer.borderColor = UIColor.systemTeal.cgColor
        self.tintColor = UIColor.systemTeal
        self.setTitleColor(UIColor.systemTeal, for: .regular)

    }

I attempted setting font colour by utilizing self.tintColor and self.setTitleColor however this doesn’t work. The chosen button exhibits up like what’s proven within the picture. I attempted passing each attainable UIControl state to setTitleColor(for:) as properly however it nonetheless doesn’t work.

How do I clear up this concern? Is the strategy I’m taking to alter the button class on click on appropriate?

ui-button-layout

[ad_2]

Leave a Reply