gpt4 book ai didi

ios - 带有圆角和边框的 UIView 边缘颜色错误

转载 作者:可可西里 更新时间:2023-11-01 03:56:59 24 4
gpt4 key购买 nike

我有一个 UIView 和两个 subview 。 subview 具有圆角和边框值。我遇到的问题是圆角边框的外边缘包含 subview 背景颜色的细线。我一定是错过了什么??

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;

最佳答案

要解决此问题,请将 subview 的背景颜色设置为 clearColor,然后使用自定义 View 类的 drawRect 方法绘制背景颜色。这是 View 类的代码。

@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

下面是您将如何使用自定义 View 类。与您发布的内容相比,这里唯一真正的变化是 subview 的背景颜色设置为 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];

关于ios - 带有圆角和边框的 UIView 边缘颜色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23073224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com