ios - 我无法使图像和按钮的角变圆

标签 ios uibutton uiimage calayer

我正在创建 UITableViewController 的单元格。 其中一个包含一个小图像。我尝试了以下方法使其角变圆:

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

在另一个单元格原型(prototype)中,我尝试使按钮的角变圆:

    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;

    }
}

但在任何情况下我都无法将角变圆。你能看出我错在哪里吗?

谢谢

最佳答案

上面的代码片段都是正确的,没有问题。我的假设是您的问题在于创建包含图像的按钮。下面的代码将创建一个按钮并将其圆角,我还为它添加了边框,以防你也想添加它。您也可以插入其中的图像。我还添加了此代码将创建的图像以供您引用。希望对您有所帮助。

  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

我知道您使用的是自定义单元格,因此请在 cellForRowAtImdexPath 或任何使用自定义单元格填充 tableview 的地方实现 maskToBount .

关于ios - 我无法使图像和按钮的角变圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17639330/

相关文章:

ios - 如何使用 Core Data 使用 NSFetchRequest 的结果填充 UIPickerView

ios - Google Analytics V3.03 不发回数据

ios - 使用 AutoLayout 在 vi​​ewDidAppear 之前获取 UIButton 宽度

iphone - 如何制作一个小的uiimage并在其上手动设置像素?

ios - 如何在 Swift 中使用 YouTube Api v3 正确检查 UIImage 是否为零?

swift - 为什么来自 UIImagePickerController 的图像有时会被错误地裁剪?

ios - 如何在不重新启动应用程序 swift4 的情况下更改本地化语言?

ios - 使用 Apple ID 登录显示响应类型无效

ios - 将 Traget 添加到 UIView 中的 UIButton。收到错误 "terminating with uncaught exception of type NSException"

ios - 如何在ios中单击按钮时更改按钮的图像