0

我正在创建 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

这是我的 uitableviewcontroller 中的函数 cellForRowAtIndexPath 的代码:

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

    }
}

但在任何情况下,我都没有设法使角落变圆。你能告诉我哪里错了吗?

谢谢

4

1 回答 1

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

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

在此处输入图像描述

编辑:

做圆角的另一种方法是使用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;
}

这是结果的屏幕截图:

在此处输入图像描述

我知道您使用自定义单元格,因此maskToBountcellForRowAtImdexPath您使用其自定义单元格填充的地方或任何地方实现tableview

于 2013-07-14T13:49:42.497 回答