Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding Properties to Attribute Inspector in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.03k
    Comment on it

    Accessing properties via interface builder is quite easy and more developer friendly.

    Here I’m guiding you how to to add new/custom properties to interface builder.

     

    1- Subclass any UIObject, we are considering here UIButton for now, as shown below:
     

    class YourCustomButton: UIButton {
    
    }
    
    2- Add properties that you want to be visible in interface builder attribute inspector for the object.
    
    class YourCustomButton: UIButton {
        
       	@IBInspectable var buttonCornerRadius:CGFloat{
            
          	set{
    		    self.layer.cornerRadius = newValue // Setting corner radius of button; newValue is changed value.
            }
            get{
                return self.layer.cornerRadius		// returning corner radius of button
            }
        } 
    }
    

    We have added buttonCornerRadius as a new property having getter and setter. Using this property we are setting corner radius of Button

     

    3- Now in interface Builder change the class of UIButton to TDCustomButton.

    4- Once you are done with changing the button class, “buttonCornerRadius” property will start appearing on the attribute inspector.

    5- Now you can change the corner radius directly from interface builder attribute inspector.

     

    If you want to get result at build time then use “@IBDesignable” before “class” as below:

    @IBDesignable class YourCustomButton: UIButton {
        
    }
    

    using @IBDesignable will show the changes immediately while changing values in interface builder.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: