【问题标题】:iOS UITableViewCell cell.imageView setting rounded corneriOS UITableViewCell cell.imageView 设置圆角
【发布时间】:2013-04-12 17:29:35
【问题描述】:

嘿,我正在尝试设置cell.imageViewcornerRadius,但它似乎不起作用。

cell.imageView.layer.cornerRadius=9;

它会起作用还是我应该在我的单元格中添加一个自定义的UIImageView 以具有圆角?

我也试过了

cell.imageView.layer.borderWidth=2;
cell.imageView.layer.borderColor=[[UIColor blackColor]CGColor];

但它似乎也不起作用。有没有人遇到过类似的问题?

【问题讨论】:

  • 您还需要设置masksToBounds。你读过CALayer 文档吗?
  • @Desdenova 对不起,我忘了一个基本的东西,谢谢:)。

标签: ios uitableview uiimageview


【解决方案1】:

首先add Framework -> QuartzCore.framework 到你的项目
然后在您的ViewController.m 文件中导入头文件

#import <QuartzCore/CALayer.h>

在您的cellForRowAtIndexPath 方法中添加以下代码:

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

    NSString *cellIdentifier = @"TableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        // Rounded Rect for cell image
        CALayer *cellImageLayer = cell.imageView.layer;
        [cellImageLayer setCornerRadius:9];
        [cellImageLayer setMasksToBounds:YES];
    }                                

    cell.imageView.image = [UIImage imageNamed:@"image_name.png"];

    return cell;
}

【讨论】:

  • 谢谢,我只是错过了 setMasksToBounds。犯了一个可怕的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 2013-08-23
  • 2011-11-17
  • 2018-09-04
  • 1970-01-01
相关资源
最近更新 更多