如何在storyboard设置圆角(cornerRadius)、边框(borderWidth)等操作。

本文介绍了两种在Storyboard中为UI元素设置圆角和边框的方法:Runtime Attributes和@IBInspectable。Runtime Attributes适用于个别控件,但易出错;而@IBInspectable结合分类在大量控件设置时更便捷。详细步骤包括创建分类,添加属性,实现setter和getter方法。总结中提到,@IBInspectable还可用于其他属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

方法1:Runtime Attributes

方法2:使用@IBInspectable(下面以UIButton为例介绍使用方法)

Step1:创建一个UIButton分类(Category)UIButton+Layer

Step2:UIButton+Layer.h 中添加属性

Step3:UIButton+Layer.m中实现setter、getter方法

Step4:到这里前期准备就做完了,选中storyboard中你的任意一个UIButton,都能在下图中简单地设置

step5:来看看效果

总结:


方法1:Runtime Attributes

设置圆角:layer.cornerRadius
设置边宽:layer.borderWidth

缺点:keyPath需要手敲,容易拼写错误,需要设置多个控件时相对比较麻烦。

 

方法2:使用@IBInspectable(下面以UIButton为例介绍使用方法

Step1:创建一个UIButton分类(Category)UIButton+Layer

Step2:UIButton+Layer.h 中添加属性

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIButton (Layer)
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;
@end

NS_ASSUME_NONNULL_END

Step3:UIButton+Layer.m中实现setter、getter方法

#import "UIButton+Layer.h"

@implementation UIButton (Layer)

/************ cornerRadius ************/
- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
}

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

/************ borderWidth ************/
- (void)setBorderWidth:(CGFloat)borderWidth {
    self.layer.borderWidth = borderWidth;
}
- (CGFloat)borderWidth {
    return self.layer.borderWidth;
}
@end

Step4:到这里前期准备就做完了,选中storyboard中你的任意一个UIButton,都能在下图中简单地设置

step5:来看看效果

*************************************************************************************************

总结:

1、方法1的Runtime Attributes在针对个别控件设置地时候还是比较适用的,需要注意拼写别出错;

2、方法2的@IBInspectable在项目中有多个控件需要设置一些属性时,结合category就显得非常方便了,一劳永逸;

3、根据@IBInspectable的特点,还可以做其它方面的延伸~

 

日常开发使用心得整理,如有纰漏,欢迎指正!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值