美文网首页
IBInspectable和IB_DESIGNABLE

IBInspectable和IB_DESIGNABLE

作者: 小雨雨儿 | 来源:发表于2016-10-09 09:26 被阅读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

相关文章

网友评论

      本文标题:IBInspectable和IB_DESIGNABLE

      本文链接:https://www.haomeiwen.com/subject/qvepyttx.html