【问题标题】:Change UIView backgrundColor reset round corners更改 UIView backgrundColor 重置圆角
【发布时间】:2017-08-09 13:09:24
【问题描述】:

我需要更改自定义类按钮的背景颜色,由 UIViewUIButton 组成。

    - (instancetype)initWithFrame:(CGRect)frame color:(UIColor*)color imageName:(NSString*)imageName target:(id)target action:(SEL)action tag:(int)tag{

    self = [super init];

    if (self) {

        self.frame = frame;

        CircleStatusView * view = [[CircleStatusView alloc]initWithFrame:self.bounds color:color];
        view.tag = 22;
        [self addSubview:view];

        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = self.bounds;
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setImage:[[UIImage imageNamed:imageName]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [btn setTintColor:[UIColor whiteColor]];
        btn.tag = tag;
        [self addSubview:btn];


    }
    return self;

}

当使用此方法按下UIButton 时,我会更改UIView 的背景颜色。我从灰色变为绿色:

 - (void)changeBackgroundColor:(UIColor*)color{

        [(CircleStatusView*)[self viewWithTag:22] setBackgroundColor:color];

    }

CircleStatusView 是这样制作的:

- (instancetype)initWithFrame:(CGRect)frame color:(UIColor*)color{

    self = [super init];

    if (self) {

        self.frame = frame;
        self.layer.borderColor = [UIColor whiteColor].CGColor;
        self.layer.borderWidth = 3;
        self.backgroundColor = color;
        self.clipsToBounds = YES;
        self.layer.masksToBounds = NO;
        self.layer.cornerRadius = frame.size.width/2;

        self.layer.shadowColor = [UIColor darkGrayColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 1.0;

    }
    return self;

}

我的问题是视图的角半径消失了,视图变成了正方形而不是保持圆形,如下所示:

【问题讨论】:

    标签: ios objective-c uiview calayer


    【解决方案1】:

    我调用的是 setBackgroundColor: 而不是 changeBackgroundColor:,愚蠢的错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2015-03-18
      • 2017-10-01
      • 2013-05-15
      • 2011-01-24
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多