0

I want to create a tableview that has some round subview as UIView. I set the UIView's layer.cornerRadius and clipsToBounds. But some views are not round. Who can help me to solve this problem or give me some advice?

My code:

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

result:

enter image description here

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Li.Baby
  • 3
  • 3

3 Answers3

0

Inside this clipsToBound is Bool so you should Give YES or NO and Try To use maskToBound = YES for Corner Radius If you want to merge the below view Than use clipsToBound accordingly And I don't think that rasterizationScale and shouldRasterize is useful here. I hope This Will Help you.

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

And If You Want To UITableViewCell rounded corners and clip subviews than Follow This Link Click here

  • The rasterizationScale and shouldRasterize is for "Off-Screen Rendering",but it has no effect on the round view. and the "view.clipsToBounds = YES" is eque to "view.layer.masksToBounds = 1". I already used the CGContextRef and UIBezierPath to create a round view on the UITableViewCell, but they can't create a real round view. – Li.Baby Aug 25 '16 at 07:02
  • Then Make Custom Cell in Storyboard and set user define Run Time Attributes I am Sure That will Help You for using user define run time attribute prefer this link http://stackoverflow.com/questions/12301256/is-it-possible-to-set-uiview-border-properties-from-interface-builder –  Aug 25 '16 at 08:28
  • Thanks for your help. I already solve this problem used the Rock's way. Only used the custom round view could solve this problem. But i don't know what the difference about them.@Kartik Singh Bhadoriya – Li.Baby Aug 26 '16 at 03:06
  • 1 up vote by me Because you solved your problem great. :-) –  Aug 26 '16 at 09:31
0

The rasterizationScale and shouldRasterize is for "Off-Screen Rendering",but it has no effect on the round view. and the "view.clipsToBounds = YES" is eque to "view.layer.masksToBounds = 1". I already used the CGContextRef and UIBezierPath to create a round view on the UITableViewCell, but they can't create a real round view.

Li.Baby
  • 3
  • 3
0

You Will Try it With Create a Custom Cell Class, and create your view not a programmatically its create using storyboard. and set property of view in custom cell class. then implement above code but a little changes here i have mention it.

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];
Rock
  • 64
  • 5
  • Thanks for your help, I solve the problem used your suggest. I don't know what the difference about our way to create a round view. The round view which used your way created more like a clrcle. Please tell me if you know it. – Li.Baby Aug 26 '16 at 02:21