IBInspectable和IB_DESIGNABLE

2016-10-09  本文已影响18人  小雨雨儿

1.使用IBInspectable可以在xib/storyboard中方便的为类设置属性并看到效果.
在.h文件中声明属性,在属性中加关键字IBInspectable

#import <UIKit/UIKit.h>

@interface XYButton : UIButton
@property (nonatomic, assign) IBInspectable CGFloat connerRadius;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;
@property (nonatomic, assign) IBInspectable UIColor *borderColor;
@end

然后在.m文件中添加IB_DESIGNABLE关键字。如果不加IB_DESIGNABLE,不能再xib中实时看到效果

#import "XYButton.h"

IB_DESIGNABLE
@implementation XYButton

- (void)setConnerRadius:(CGFloat)connerRadius {
    _connerRadius = connerRadius;
    self.layer.cornerRadius = _connerRadius;
    self.layer.masksToBounds = YES;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    self.layer.borderWidth = _borderWidth;
}

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    self.layer.borderColor = _borderColor.CGColor;
}

@end

效果:

test.png
上一篇下一篇

猜你喜欢

热点阅读