New Features of iOS8 IBDesignable

Source: Internet
Author: User

New Features of iOS8 IBDesignable
New OS8 features: IBDesignable/IBInspectable. You can directly set the attributes of the UI class in XIB or Storyboard. For example, UIView. layer. borderWidth, borderColor, and cornerRadius attributes cannot be directly set on XIB, but IBDesignable/IBInspectable can map these attributes to XIB using the runtime mechanism, at the same time, the custom attributes of our UI class can also be mapped. I will not talk much about the many explanations. Let's take a look at what Nate Cook wrote. Take the subclass of UIView as an example: Swift IBDesignable/IBInspectable first we need to create a ViewSwift class. View inherits from UIView and selects the Swift language.

@IBDesignableclass View: UIView {        @IBInspectable var cornerRadius: CGFloat = 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        }    }}

 

Drag a UIView in the xib or Storyboard, change the class to a custom View, and click Edit next to it to View the attributes we have Dynamically Loaded, you can modify the View directly, and the View on the XIB will also change. We can see that the subclass of A UIView becomes Objective-C IBDesignable/IBInspectable to create a ViewOC class. ViewOC inherits from UIView and selects Objective-C language. h declares the attribute @ property (nonatomic, assign) IBInspectable CGFloat cornerRadius; @ property (nonatomic, assign) IBInspectable CGFloat bwidth; @ property (nonatomic, assign) IBInspectable UIColor * bcolor. Note: IBInspectable modifier location. In the. m file, use IB_DESIGNABLE before @ implementation. IB_DESIGNABLE @ implementation ViewOC, and then assign a value in the set method, It will be OK, as shown below:
- (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 that of Swift. If you have any questions or suggestions, you are welcome to provide guidance and questions.

Related Article

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.