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:
1. The very first thing you do is to create a Xcode project by usual setting of Single View Application & selecting the language Swift.
Notice that the class is extending its super class UIView && the attribute "@IBDesignable" is also add to its declaration. Function init(frame:) is the initializer function that is calling user defined function "designingLabel()", which is doing the main task of programmatically drawing the label on the screen. And init(coder:) is the function to deserializer of out label view. The "layoutSubviews()" is used here so that view will load properly even if we are checking the changes made by us in Interface Builder File. Now that all is set go ahead && compile you app as usual simulator will show up && present a label with green color, what else anew, now go to Main.Storyboard file && click on one and only View then you will see the same label with green color. This is what @IBDesignable does. Here is figure that will display the result:
1. The very first thing you do is to create a Xcode project by usual setting of Single View Application & selecting the language Swift.
3. Now Create a new Cocoa Touch Class file call it testingIBDesignable & make it a subclass of UIView. Now open the testingIBDesignable.swift file. Now open the Main.Storyboard file && click on the View not the document outline && change the custom class to testingIBDesignable. Then add the following sort of code to testingIBDesignable.swift file...
import UIKit
@IBDesignable class testingIBDesignable: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
designingLabel()
}
override init(frame: CGRect) {
super.init(frame: frame)
designingLabel()
}
override func layoutSubviews() {
designingLabel()
}
func designingLabel() {
var label = UILabel()
label.frame = CGRectMake(50, 50, 200,30)
label.backgroundColor = UIColor.greenColor()
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Left
label.text = "" //nil text
addSubview(label)
}
}
It is the picture of my storyboard file && showing the output, green label will "nil" text.