簡體   English   中英

UITableViewCell的子視圖不能是圓形的

[英]UITableViewCell's subviews can't be round

我想創建一個具有一些圓形子視圖作為UIView的表視圖。 我設置了UIView的layer.cornerRadius和clipsToBounds。 但是有些觀點並不圓。 誰可以幫助我解決這個問題或給我一些建議?

我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * settCellID = @"settingCellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID];

        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
        view.layer.cornerRadius = 10;
        view.clipsToBounds = 1;
        view.layer.rasterizationScale = [UIScreen mainScreen].scale;
        view.layer.shouldRasterize = 1;
        view.backgroundColor = [UIColor blueColor];
        [cell.contentView addSubview:view];
    }
    return cell;
}

結果:

在此處輸入圖片說明

在clipsToBound內部是Bool,因此您應輸入YES或NO並嘗試對Corner Radius使用maskToBound = YES如果要合並下面的視圖,則不應該相應地使用clipsToBound,並且我認為rasterizationScale和shouldRasterize在這里不是有用的。 我希望這能幫到您。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * settCellID = @"settingCellID";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID];

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
    view.layer.cornerRadius = 10;
    view.clipsToBounds = YES;
    view.maskToBounds =YES;
    view.layer.rasterizationScale = [UIScreen mainScreen].scale;
    view.layer.shouldRasterize = 1;
    view.backgroundColor = [UIColor blueColor];
    [cell.contentView addSubview:view];
}
return cell;
}

並且如果您想將UITableViewCell的圓角和剪輯子視圖比起,請點擊這里

rasterizationScale和shouldRasterize用於“屏幕外渲染”,但對圓形視圖沒有影響。 並且“ view.clipsToBounds = YES”等同於“ view.layer.masksToBounds = 1”。 我已經使用CGContextRef和UIBezierPath在UITableViewCell上創建了一個圓形視圖,但是它們不能創建一個真正的圓形視圖。

您將嘗試使用創建自定義單元格類來進行嘗試,而不是通過編程方式使用情節提要創建視圖。 並在自定義單元格類中設置視圖屬性。 然后實現上面的代碼,但在這里我作了一些改動。

static NSString * settCellID = @"settingCellID";
'CustomClassName' * cell = (CustomClassName*)[tableView dequeueReusableCellWithIdentifier:settCellID];
cell.viewPropertyName.layer.cornerRadius = 10;
cell.viewPropertyName.clipsToBounds = YES;
cell.viewPropertyName.maskToBounds =YES;
cell.viewPropertyName.backgroundColor = [UIColor blueColor];

暫無
暫無

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

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