為了實(shí)現(xiàn)UITableView兩側(cè)留白的效果,嘗試了以下三種方法:
一序厉、只單獨(dú)設(shè)置contentInset,由于cell寬度不變矫户,horizonal content會(huì)超過屏幕寬度
代碼:
self.tableView.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
APP截圖
二离斩、在UITableViewCell的子類中,重寫- (void)SetFrame : (CGRect)frame方法
注:使用setFrame后抽高,在左滑cell的時(shí)候會(huì)出現(xiàn)錯(cuò)誤判耕!cell的frame是table去決定的,不能更改3凇F盹酢渺贤!
代碼:
- (void)setFrame:(CGRect)frame {
frame.origin.x += 8;
frame.size.width -= 16;
[super setFrame:frame];
}
APP截圖
三、在UITableViewCell的子類中请毛,重寫初始化方法志鞍,添加一個(gè)subview,設(shè)置其frame方仿,將其置于最底部固棚,同時(shí)將cell的backgroundcolor設(shè)為透明
- (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;
}
APP截圖