Pages

Showing posts with label @IBDesignable && @IBInspectable. Show all posts
Showing posts with label @IBDesignable && @IBInspectable. Show all posts

Editing object's properties in Interface Builder

As you guys know that you can test your views directly in your interface builder, then think should't you have a way to edit attributes related to your hand created objects directly in you Interface Builder. Well there is a way which will let you edit different attributes of you objects in  Interface Builder. This feature allows the view to be tested without running your application again and again. The attribute that you want to edit/change should be marked as "@IBInspectable" && class is marked with "@IBDesignable", also class should extend UIView class. Let us understand it with an example in which we  want to create a label programmatically && the check different changes that we will make:

Testing the User Interface in Xcode

Well guys up till now you are forced to compile your project to see the effect of the slightest change that you made in your project. But Swift provides some attributes that will let you test your views directly in your Interface Builder. The class corresponding to the view you want to test should be extending the UIView class && also should be marked with "@IBDesignable" attribute. By adding this attribute in front of the declaration of the class we are permitting Xcode to instantiate && run the View, also Xcode will update the view according to the changes we will be making into our class corresponding to the view. As if our class is marked with the attribute @IBDesignable then Xcode will attempt to load the view && display this view in the storyboard or in the xib file that you are using to check these changes. Again one more thing to note is that when class loads, the User Interface will not load properly. To do this override the layOutSubView() method to call the function that is responsible for designing our view. For example we  want to create a label programmatically && the check different changes that we will make: