簡體   English   中英

更改UIView backgrundColor重置圓角

[英]Change UIView backgrundColor reset round corners

我需要更改由UIViewUIButton組成的自定義類按鈕的bg顏色。

    - (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的backgroundColor。 我從灰色變為綠色:

 - (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;

}

我的問題是,視圖的角半徑消失了,視圖變成了正方形而不是保持圓形,如下所示:

在此處輸入圖片說明

我正在打電話給setBackgroundColor:而不是changeBackgroundColor :,啞巴錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM