我想在 View 中添加一个头部按钮,周围有一些空白区域。我的代码是这样的:
// 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];
但它周围总是有锯齿。它可能只有一个像素。但是看起来很可怕。像这样:
有人有一些技巧可以去除黑色锯齿吗?
最佳答案
派对迟到了,但从过去的经验来看,这是一个角落半径和边界的错误。我尝试使用它在 View 周围创建圆形边框,并注意到类似的出血。
一种解决方法是为边框创建一个 View ,然后为图像创建一个稍微嵌入的 View 。
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];
如果您想避免多个 View ,您可以在 ImageView 的边框层中添加一个额外的背景层。那将需要将其边界反向插入,以便在图像周围绘制。
关于iphone - 使用 Quartz 时如何去掉 UIView 的锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783814/