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.