ios8x new Features Ibdesignable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.