OStack程序员社区-中国程序员成长平台

标题: ios - 颜色问题外的惊人圆角半径 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:48
标题: ios - 颜色问题外的惊人圆角半径

这是我添加到新 View Controller 的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];
    [contentView setClipsToBounds:YES];
    [contentView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:contentView];

    [[contentView layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
    [[contentView layer] setBorderWidth:1.0f];
    [[contentView layer] setCornerRadius:5.0f];
    [[contentView layer] setMasksToBounds:YES];
} 

结果:

enter image description here

如果看角落,我们可以看到外面的蓝色像素:

enter image description here



Best Answer-推荐答案


您可以使用 CAShapeLayer :

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];
    [self.view addSubview:contentView];

    CAShapeLayer *subLayer = [[CAShapeLayer alloc] init];
    [subLayer setFillColor:[UIColor blueColor].CGColor];
    [subLayer setStrokeColor:[UIColor grayColor].CGColor];
    [subLayer setLineWidth:6.0];
    [subLayer setPath:[UIBezierPath bezierPathWithRoundedRect:contentView.bounds cornerRadius:5.0].CGPath];

    [contentView.layer addSublayer:subLayer];
}

关于ios - 颜色问题外的惊人圆角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29213208/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4