Search code examples
iosuibuttonuiimagecalayer

I can't make corners of images and buttons rounded


I'm creating the cells of a UITableViewController. In one of them, a small image is contained. I tried the following to make its corners rounded:

profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;

In another cell prototype, I tried to make the corners of a button rounded:

    chooseImageFromRoll.clipsToBounds = YES;
    chooseImageFromRoll.layer.cornerRadius = 8;

In both cases I included

#import <QuartzCore/QuartzCore.h>

The button and the image whose corners must be rounded are property of the UITableViewController owning them:

#import <UIKit/UIKit.h>

@interface profileRegistrationCellVC : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *profileImage;
@property (weak, nonatomic) IBOutlet UIButton *chooseImageFromRoll;
@property (weak, nonatomic) IBOutlet UIButton *shootImage;

@end

In relative .m class:

#import "profileRegistrationCellVC.h"
#import <QuartzCore/QuartzCore.h>

@implementation profileRegistrationCellVC
@synthesize profileImage;
@synthesize chooseImageFromRoll;
@synthesize shootImage;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        chooseImageFromRoll.clipsToBounds = YES;
        chooseImageFromRoll.layer.cornerRadius = 8;
        shootImage.layer.cornerRadius = 8;

        profileImage.layer.cornerRadius = 8;
        profileImage.clipsToBounds = YES;

        profileImage.layer.borderColor = [UIColor whiteColor].CGColor;
        profileImage.layer.borderWidth = 20.0;

        [self addSubview:profileImage];
        [self addSubview:chooseImageFromRoll];
        [self addSubview:shootImage];


    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Here's the code of my function cellForRowAtIndexPath, in my uitableviewcontroller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"profileRegistrationCell";

        profileRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[profileRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        //cell.profileImage.image.layer.masksToBounds = YES;
        //cell.profileImage.layer.cornerRadius = 5.0;

        return cell;

    }

    else if (indexPath.section == 1) {
        static NSString *CellIdentifier = @"regularRegistrationCell";

        regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.regularFieldName.text = [_registrationItems objectAtIndex:indexPath.row];

        if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:@"Email*"])
            cell.regularTextField.keyboardType = UIKeyboardTypeEmailAddress;

        if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:@"Età"]) {
            cell.regularTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        }

        return cell;

    }

    else{
        static NSString *CellIdentifier = @"orientationRegistrationCell";

        orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.fieldLabel.text = [_registrationItems objectAtIndex:[_registrationItems count]-1];
        cell.orientationLabel.text = @"Non specificato";
        return cell;

    }
}

But in no case I managed to make corners rounded. Can you tell where I'm mistaking?

Thanks


Solution

  • The code snippets above are both correct and have no issues. My assumption is that your issue lies elsewhere in creating the button that contains the image. The following code will create a button and will round it's corners, also I added the border to it in case you want to add that too. You can plug in the image you have in there as well. I have added a image of what this code will create for your reffrence also. Hope it will help you out.

      UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
      btn.frame = CGRectMake(100, 100, 100,50);
      [btn setTitle:@"Hello" forState:UIControlStateNormal];
      [btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f  blue:0.0/255.0f alpha:0.7]];
       btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same  value
       btn.clipsToBounds = YES;
    
       btn.layer.cornerRadius = 20;//half of the width
       btn.layer.borderColor=[UIColor redColor].CGColor;
       btn.layer.borderWidth=2.0f;
    
       [self.view addSubview:btn];
    

    Below is the image of the button that is related with the above code

    enter image description here

    Edit:

    another way of doing the round corners is to use the method masksToBounds here is an example of it within the generic cell right out of the box from the template.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    cell.imageView.image = [UIImage imageNamed:@"acolon.png"];
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 5.0;
    return cell;
    }
    

    here is the screen shot of the result:

    enter image description here

    i know you using a custom cell, so implement the maskToBount in the cellForRowAtImdexPath or wherever you are populating the tableview with its custom cells.