带有圆角和边框的UIView有错误的边缘颜色 [英] UIView with rounded corner and border has wrong edge color

查看:98
本文介绍了带有圆角和边框的UIView有错误的边缘颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView和两个子视图。子视图具有圆角和边框值。我有的问题是,圆角边框的外边缘包含子视图背景颜色的细线。我必须缺少一些东西??

I have a UIView and two subviews in it. The subviews have rounded corners and a border value. The issue I have is that the outer edges of the rounded border contain a thin line of the subview background color. I must be missing something??

UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 320)];
[self.view addSubview:outerView];
outerView.backgroundColor = [UIColor whiteColor];

UIView *innerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 320)];
[outerView addSubview:innerView1];
innerView1.backgroundColor = [UIColor blackColor];
innerView1.layer.borderWidth = 20;
innerView1.layer.borderColor = [UIColor whiteColor].CGColor;
innerView1.layer.cornerRadius = 20;
//innerView1.layer.masksToBounds = YES;

UIView *innerView2 = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, 320)];
[outerView addSubview:innerView2];
innerView2.backgroundColor = [UIColor blackColor];
innerView2.layer.borderWidth = 20;
innerView2.layer.borderColor = [UIColor whiteColor].CGColor;
innerView2.layer.cornerRadius = 20;
//innerView2.layer.masksToBounds = NO;
//innerView2.clipsToBounds = YES;
//innerView2.layer.shouldRasterize = YES;


推荐答案

要解决此问题,请设置背景颜色子视图到 clearColor ,然后使用自定义视图类的 drawRect 方法绘制背景颜色。下面是视图类的代码。

To work around the problem, set the background color of the subviews to clearColor and then draw the background color using the drawRect method of a custom view class. Here's the code for the view class.

@interface WorkAroundView : UIView
@end

@implementation WorkAroundView
- (void)drawRect:(CGRect)rect
{
    CGFloat margin = self.layer.borderWidth;
    CGRect  background;
    background.origin.x = margin;
    background.origin.y = margin;
    background.size.width  = self.bounds.size.width  - 2 * margin;
    background.size.height = self.bounds.size.height - 2 * margin;

    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect( context, background );
}
@end

下面是如何使用自定义视图类。这里从你发布的唯一的真正的变化是subview的背景颜色设置为clearColor。

And here's how you would use the custom view class. The only real change here from what you posted is that the background color for the subviews is set to clearColor.

UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(360, 200, 320, 320)];
[self.view addSubview:outerView];
outerView.backgroundColor = [UIColor whiteColor];

WorkAroundView *innerView1 = [[WorkAroundView alloc] initWithFrame:CGRectMake(0, 0, 160, 320)];
innerView1.backgroundColor = [UIColor clearColor];
innerView1.layer.borderWidth = 20;
innerView1.layer.borderColor = [UIColor whiteColor].CGColor;
innerView1.layer.cornerRadius = 20;
[outerView addSubview:innerView1];

WorkAroundView *innerView2 = [[WorkAroundView alloc] initWithFrame:CGRectMake(160, 0, 160, 320)];
innerView2.backgroundColor = [UIColor clearColor];
innerView2.layer.borderWidth = 20;
innerView2.layer.borderColor = [UIColor whiteColor].CGColor;
innerView2.layer.cornerRadius = 20;
[outerView addSubview:innerView2];

这篇关于带有圆角和边框的UIView有错误的边缘颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆