UITableView左右留白
2017-06-12 本文已影响0人
邪恶de鱼
为了实现UITableView两侧留白的效果,尝试了以下三种方法:
一、只单独设置contentInset,由于cell宽度不变,horizonal content会超过屏幕宽度
代码:
self.tableView.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);

二、在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];
}

三、在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;
}
