2

我想将一个头部按钮添加到一个周围有一些空白区域的视图中。我的代码是这样的:

// head button
UIButton * btnHead = [UIButton buttonWithType:UIButtonTypeCustom];
btnHead.frame = CGRectMake(0, 0, self.frame.size.width - property.userhead_cell_space / 2, self.frame.size.width  - property.userhead_cell_space / 2);
btnHead.clipsToBounds = YES;
btnHead.layer.cornerRadius = btnHead.bounds.size.width / 2;
btnHead.layer.borderColor = [UIColor whiteColor].CGColor;
btnHead.layer.borderWidth = (isPad?4.0f:2.0f);
btnHead.layer.contentsScale = [[UIScreen mainScreen] scale];
btnHead.layer.backgroundColor = [UIColor whiteColor].CGColor;

[btnHead addTarget:self action:@selector(clickHead:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:btnHead];

但它周围总是有锯齿。它可能只有一个像素。但是看起来很可怕。像这样: 头部按钮周围有黑色锯齿

有人有一些技巧可以去除黑色锯齿吗?

4

1 回答 1

0

派对迟到了,但从过去的经验来看,这是一个角落半径和边界的错误。我尝试使用它在视图周围创建圆形边框,并注意到类似的出血。

一种解决方法是为边框创建一个视图,然后为图像创建一个稍微嵌入的视图。

UIView* border = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
border.backgroundColor = UIColor.whiteColor; // border color
border.layer.cornerRadius = border.bounds.size.width / 2;
border.layer.masksToBounds = YES;

// inset by the desired border width
UIImageView* image = [[UIImageView alloc] initWithFrame:CGRectInset(border.bounds, 2, 2)];
image.image = someImage;
image.layer.cornerRadius = image.bounds.size.width / 2;
image.layer.masksToBounds = YES;

[border addSubview:image];
[self addSubview:border];

如果您想避免多个视图,您可以在图像视图的边框层中添加一个额外的背景层。那将需要将其边界反向插入,以便在图像周围绘制。

于 2014-10-08T18:43:26.000 回答