在我的应用程序中,我需要创建一个带有 1px 彩色边框和圆角半径的按钮。宽度可能不同。
当然我会创建一个 UIButton 设置它的 layer.cornerRadius 和 layer.borderWidth。
它是最优的吗? 有没有更好的方法来实现这一目标?
另一位开发人员告诉我,这样做的成本很高。
你怎么看?
谢谢。
最佳答案
子类 UIButton。可以命名为 ViewWithThinBorder
。
在 awakeFromNib 方法的实现中添加边框角和宽度属性。
现在您可以转到界面构建器选择按钮并将其类设置为身份检查器中的 ViewWithThinBorder
。
这不仅优化了它在界面构建器周围任何地方的可重用性。您还可以从中央位置更改所有 View 层次结构周围的按钮。
#import "ViewWithThinBorder.h"
@implementation ViewWithThinBorder
-(void)awakeFromNib
{
self.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
self.layer.borderWidth = 1.0f;
self.layer.cornerRadius = 1.0f;
}
@end
关于ios - 带边框的按钮(最优化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29305259/