为了实现UITableView两侧留白的效果,尝试了以下三种方法:
一、只单独设置contentInset,由于cell宽度不变,horizonal content会超过屏幕宽度
代码:
self.tableView.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
![](https://img.haomeiwen.com/i3362183/ab2497c55e2ba208.png)
二、在UITableViewCell的子类中,重写- (void)SetFrame : (CGRect)frame方法
注:使用setFrame后,在左滑cell的时候会出现错误!cell的frame是table去决定的,不能更改!!!
代码:
- (void)setFrame:(CGRect)frame {
frame.origin.x += 8;
frame.size.width -= 16;
[super setFrame:frame];
}
![](https://img.haomeiwen.com/i3362183/993f2ad81658112e.png)
三、在UITableViewCell的子类中,重写初始化方法,添加一个subview,设置其frame,将其置于最底部,同时将cell的backgroundcolor设为透明
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(self.left + 8, self.top, self.width - 16, self.height)];
backView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
[self addSubview:backView];
[self sendSubviewToBack:backView];
return self;
}
![](https://img.haomeiwen.com/i3362183/4d3d7413c7de7880.png)
网友评论