我不能让图像和按钮的角落四舍五入

时间:2013-07-14 12:21:11

标签: ios uibutton uiimage calayer

我正在创建UITableViewController的单元格。 在其中一个中,包含一个小图像。我尝试了以下方法使其角落四舍五入:

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

在另一个细胞原型中,我试图将按钮的角落四舍五入:

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

在这两种情况下,我都包括

#import <QuartzCore/QuartzCore.h>

按钮和角点必须圆角的图像是拥有它们的UITableViewController的属性:

#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

在相对.m级:

#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

这是我的函数cellForRowAtIndexPath的代码,在我的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;

    }
}

但在任何情况下我都没有设法让角落四舍五入。你能说出我在哪里错了吗?

由于

1 个答案:

答案 0 :(得分:3)

上面的代码段都是正确的,没有问题。我的假设是你的问题出在创建包含图像的按钮的其他地方。下面的代码将创建一个按钮并将其圆角,我也添加了边框,以防你想要添加它。您也可以插入您在那里的图像。我已添加了此代码将为您的内容创建的图像。希望它会帮助你。

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

以下是与上述代码相关的按钮图像

enter image description here

编辑:

执行圆角的另一种方法是使用方法masksToBounds这里是一个从模板开箱即用的通用单元格中的示例。

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

这是结果的屏幕截图:

enter image description here

我知道您使用的是自定义单元格,因此请在maskToBountcellForRowAtImdexPath及其自定义单元格填充的任何位置实施tableview