美文网首页
UITableView左右留白

UITableView左右留白

作者: 邪恶de鱼 | 来源:发表于2017-06-12 15:45 被阅读0次

为了实现UITableView两侧留白的效果,尝试了以下三种方法:

一、只单独设置contentInset,由于cell宽度不变,horizonal content会超过屏幕宽度

代码:
self.tableView.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);

APP截图

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

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

相关文章

网友评论

      本文标题:UITableView左右留白

      本文链接:https://www.haomeiwen.com/subject/dsvlqxtx.html