vi 查看最顶部_目标c-仅在UIVi顶部具有圆角

我在Ashley的帮助下解决了这个问题。

首先,我将UIView子类化。 为我的班级创建一个自己的构造函数,称为- (void)layoutSubviews。在此构造函数中,我确定我要设置样式的表单元格。

然后,我覆盖- (void)layoutSubviews以创建CAShapeLayer并应用图层蒙版。

.h文件代码

typedef enum {

tableCellMiddle,

tableCellTop,

tableCellBottom,

tableCellSingle

} tableCellPositionValue;

@interface TableCellBackgrounds : UIView

{

tableCellPositionValue position;

}

- (id)initWithContentView:(UIView *)aView forTableView:(UITableView *)table andIndex:(NSIndexPath *)indexPath;

@end

.m文件代码

- (id)initWithContentView:(UIView *)aView forTableView:(UITableView *)table andIndex:(NSIndexPath *)indexPath

{

self = [super initWithFrame:aView.frame];

[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

if(self)

{

[self setBackgroundColor:[UIColor colorWithRed:(float)230/255 green:(float)80/255 blue:(float)70/255 alpha:1]];

if(table.style == UITableViewStyleGrouped)

{

int rows = [table numberOfRowsInSection:indexPath.section];

if(indexPath.row == 0 && rows == 1)

{

self.layer.cornerRadius = 11;

position = tableCellSingle;

}

else if (indexPath.row == 0)

position = tableCellTop;

else if (indexPath.row != rows - 1)

position = tableCellMiddle;

else

position = tableCellBottom;

}

}

return self;

}

- (void)layoutSubviews

{

[super layoutSubviews];

if(position == tableCellTop)

{

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:(CGSize){10.0, 10.0}].CGPath;

self.layer.mask = maskLayer;

}

else if (position == tableCellBottom)

{

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:(CGSize){10.0, 10.0}].CGPath;

self.layer.mask = maskLayer;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值