我必须为多个按钮设置边框并调整插图,所以我为UIButton写了一个类别类来添加边框并调整边缘插图。
这是我的ui button catgory代码
@implementation UIButton (BorderAndEdgeInsets)
- (void) addBorderOnSides:(UITableViewcellSides )sides
withColour:(UIColor *)colour
ofWidth:(float)width{
CALayer *border = [CALayer layer];
border.backgroundColor = [colour CGColor];
if(sides & Top)
{
border.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, width);
[self.layer addSublayer:border];
}
if(sides & Right)
{
border.frame = CGRectMake(self.frame.size.width - width, self.frame.origin.y, width, self.frame.size.height);
[self.layer addSublayer:border];
}
if(sides & Bottom)
{
border.frame = CGRectMake(self.frame.origin.x, self.frame.size.height - width, self.frame.size.width, width);
[self.layer addSublayer:border];
}
if(sides & Left)
{
border.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width, self.frame.size.height);
[self.layer addSublayer:border];
}
}
- (void) adjustImageAndTitleSpacings{
[[self titleLabel] setTextAlignment:NSTextAlignmentCenter];
[self setImageEdgeInsets:UIEdgeInsetsMake(0, 50, 20, 0)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(25, -15, 0, 0)];
}
这是我对按钮的用法:
int leftboundary = 0;
for (NSDictionary *tab in tabDetails) {
UIButton *tabButton = [[UIButton alloc]initWithFrame:CGRectMake(leftboundary, 0, 130, 66)];
leftboundary += 130;
[[tabButton titleLabel] setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[tabButton addBorderOnSides:Right withColour:[UIColor darkGrayColor] ofWidth:2];
[tabButton addBorderOnSides:Top withColour:[UIColor whiteColor] ofWidth:5];
[tabButton adjustImageAndTitleSpacings];
但在我的输出中,顶部白色边框仅在备用按钮上,而右边框在所有按钮上都是一致的。
我在这里做错了什么? 提前谢谢。