美文网首页
IBInspectable 和 IB_DESIGNABLE

IBInspectable 和 IB_DESIGNABLE

作者: 幸运1314 | 来源:发表于2017-08-28 00:18 被阅读0次

    IBInspectable storyboard 中直接修改边框颜色和边框宽度
    IB_DESIGNABLE storyboard 中直接显示出圆角效果

    效果图

    storyboard 中直接修改边框颜色和边框宽度
    

    <img src=http://www.boitx.org/ios/IBInspectable/1.gif width=600>

    storyboard 中直接显示出圆角效果
    

    <img src=http://www.boitx.org/ios/IBInspectable/2.gif width=900>

    IBInspectable

    用 IBInspectable 声明变量,变量会直接显示到 storyboard 中 属性检查器里面,会像修改 view 颜色一样方便的进行修改。

    @property (nonatomic,strong) IBInspectable UIColor *borderC;
    @property (nonatomic,assign) IBInspectable CGFloat borderWidth;
    

    IB_DESIGNABLE

    声明类,对类进行自定义,自定义效果会直接显示到 storyboard 中。

    IB_DESIGNABLE
    
    @interface BorderView : UIView
    

    核心代码

    BorderView.h
    
    #import <UIKit/UIKit.h>
    
    
    IB_DESIGNABLE
    
    @interface BorderView : UIView
    
    @property (nonatomic,strong) IBInspectable UIColor *borderC;
    
    @property (nonatomic,assign) IBInspectable CGFloat borderWidth;
    
    
    @end
    
    BorderView.m
    
    #import "BorderView.h"
    
    @implementation BorderView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            self.layer.masksToBounds = YES;
            self.layer.cornerRadius = 50;
        }
        return self;
    }
    
    - (void)setBorderC:(UIColor *)borderC
    {
        _borderC = borderC;
        self.layer.borderColor = borderC.CGColor;
    }
    
    - (void)setBorderWidth:(CGFloat)borderWidth
    {
        _borderWidth = borderWidth;
        self.layer.borderWidth = borderWidth;
    }
    
    @end
    

    下载demo

    相关文章

      网友评论

          本文标题:IBInspectable 和 IB_DESIGNABLE

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