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.09k
    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:
     

    1. class YourCustomButton: UIButton {
    2.  
    3. }
    4.  
    5. 2- Add properties that you want to be visible in interface builder attribute inspector for the object.
    6.  
    7. class YourCustomButton: UIButton {
    8.     @IBInspectable var buttonCornerRadius:CGFloat{
    9.     set{
    10.          self.layer.cornerRadius = newValue // Setting corner radius of button; newValue is changed value.
    11. }
    12. get{
    13. return self.layer.cornerRadius        // returning corner radius of button
    14. }
    15. }
    16. }
    17.  

    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:

    1. @IBDesignable class YourCustomButton: UIButton {
    2. }
    3.  

    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
Reset Password
Fill out the form below and reset your password: