skip to Main Content

Is it possible to make one style for UIButton (or any UI component) and set that style for selected buttons from Xcode Interface (storyboard)?

Like in web-development you create one css class and use in any elements.

2

Answers


  1. It is possible to style all components of the same type by using UIAppearance. When it comes to styling individual components it’s best to declare some common styling functions and then apply them on specific components. You could also create subclasses and style them individually.

    Login or Signup to reply.
  2. Yes, this possible. You need to make a custom button class, which is a subclass of
    UIButton. For example, the code below sets the background color

    class CustomButton: UIButton {
      override func awakeFromNib() {
        super.awakeFromNib()
        setUpView()
      }
      override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setUpView()
      }
      func setUpView() {
        self.layer.cornerRadius = self.frame.width/2
        self.layer.masksToBounds = true
        self.backgroundColor = UIColor.red
      }
    }
    
    

    Then in Xcode Interface (storyboard), for every button, you wish to use this style.
    Set the Custom Class in the identity inspector.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search