Using Ibdesignable/ibinspectable in OC and Swift
IOS8 The new feature ibdesignable/ibinspectable, you can directly set the properties of the UI class directly in Xib or storyboard.
For example: UIView.layer.borderWidth, BorderColor, Cornerradius These properties are not directly set on the Xib, but ibdesignable/ibinspectable, using the runtime mechanism, You can map these properties to Xib, and the custom properties of our UI classes can be mapped. I don't have much to say about too many explanations to see what Nate Cook wrote.
Take the subclass of UIView as an example: Swift ibdesignable/ibinspectable
First we have to build a Viewswift class, view inherits from UIView, select Swift Language.
@IBDesignableclass View: UIView { 0.0 { didSet { layer.cornerRadius = cornerRadius layer.masksToBounds = true } } @IBInspectable var borderColor: UIColor = UIColor() { didSet { layer.borderColor = borderColor.CGColor } } @IBInspectable var borderWidth: CGFloat = 0.0 { didSet { layer.borderWidth = borderWidth } }}
Then, in Xib or storyboard, drag a uiview up and change the class to the view you defined.
Then click on the property edit next, you can see our dynamically loaded properties, directly modify it, xib on the view will follow the change.
And we can see that a uiview subclass becomes like this.
Objective-c ibdesignable/ibinspectable
Build a Viewoc class, Viewoc inherit from UIView, choose Objective-c Language, declare the attribute in. h
@property (nonatomic, assign)IBInspectable CGFloat cornerRadius;@property (nonatomic, assign)IBInspectable CGFloat bwidth;@property (nonatomic, assign)IBInspectable UIColor *bcolor;
Note: The location of the ibinspectable decoration.
Then, in the. m file, the @implementation front is decorated with ib_designable.
IB_DESIGNABLE@implementation ViewOC
Then the set method inside to assign value is OK, as follows:
- (void)setCornerRadius:(CGFloat)cornerRadius{ _cornerRadius = cornerRadius; self.layer.cornerRadius = _cornerRadius; self.layer.masksToBounds = YES;}- (void)setBcolor:(UIColor *)bcolor{ _bcolor = bcolor; self.layer.borderColor = _bcolor.CGColor;}- (void)setBwidth:(CGFloat)bwidth { _bwidth = bwidth; self.layer.borderWidth = _bwidth;}
The effect is the same as above swift, and if you have questions and suggestions, you are welcome to instruct and ask questions.
ios8x new Features Ibdesignable