XIB中设置圆角等属性

开发过程中无可避免用到XIB,一些图片需要设置圆角之类的属性,但是XIB中并类似下面图片的中功能没有直接可以设置。这时候需要给view添加一个类别,然后在拓展这些属性进去


屏幕快照 2017-05-24 下午10.16.16.png

给UIView添加HFIBnspectable类别,然后在.h文件中重新定义cornerRadius和borderWidth属性

#import <UIKit/UIKit.h>
@interface UIView (HFIBnspectable)
@property (nonatomic,assign)IBInspectable NSInteger cornerRadius;
@property (nonatomic,assign)IBInspectable NSInteger borderWidth;
@property (nonatomic,assign)IBInspectable BOOL masksToBounds;
@end

然后在.m文件中重写这两个属性的set方法和get方法

#import "UIView+HFIBnspectable.h"

@implementation UIView (HFIBnspectable)

#pragma mark - setCornerRadius/borderWidth

-(void)setCornerRadius:(NSInteger)cornerRadius
{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0;
}
-(NSInteger)cornerRadius
{
    return self.layer.cornerRadius;
}
-(void)setBorderWidth:(NSInteger)borderWidth
{
    self.layer.borderWidth = borderWidth;
}

-(NSInteger)borderWidth
{
    return self.layer.borderWidth;
}


-(CGColorRef)borderColor
{
    return self.layer.borderColor;
}
-(void)setMasksToBounds:(BOOL)masksToBounds
{
    self.layer.masksToBounds = masksToBounds;
}

-(BOOL)masksToBounds
{
    return self.layer.masksToBounds;
}
@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容