首页 文章

UIView的圆角不精确

提问于
浏览
2

我在UIView上有圆角的奇怪问题 . As you can see in the screenshots, the black corner is not exactly precise, 这意味着可以看到底层视图的一些像素(即角落中的白色像素用黄色箭头突出显示) .

我已设置角半径,如下所示:

contentView.layer.cornerRadius = 5.0f;
contentView.layer.masksToBounds = YES;

contentView是一个UIView对象,它包含一个UIImageView作为子视图(显示当前的Google图像) . 此外,contentView作为子视图添加到屏幕截图中显示的主视图中 .

我已经尝试过几个没有令人满意的结果的东西,包括:

  • 将角半径直接添加到主视图,而不是内容视图

  • 增大/减小边框大小和圆角半径的值

  • 将所有提到的视图的背景颜色更改为黑色,白色和透明(目前很清楚)

我感谢您的帮助 . 谢谢!

Screenshots

1

2

2 回答

  • 0

    我试过以下代码,对我来说很好

    UIView *vi = [[[UIView alloc] initWithFrame:frame] autorelease];
    vi.backgroundColor = [UIColor clearColor];
    [self addSubview:vi];
    
    UIImageView *imgv = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)] autorelease];
    imgv.layer.cornerRadius = 4.0;
    imgv.layer.borderColor = [UIColor whiteColor].CGColor;
    imgv.clipsToBounds = YES;
    imgv.image = [UIImage imageNamed:@"xxxxxx.png"];
    [vi addSubview:imgv];
    

    希望它对你有所帮助 .

  • 1

    请在代码中尝试此操作:

    contentView.layer.borderWidth=1;
    contentView.layer.borderColor=[UIColor blackColor].CGColor;
    contentView.layer.cornerRadius=5;
    

相关问题