先放效果圖
正常創(chuàng)建一個tableView
tableView不用masonry去創(chuàng)建岖是。
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.contentInset = UIEdgeInsetsMake(200,0, 0, 0);
_tableView.showsHorizontalScrollIndicator = NO;
_tableView.showsVerticalScrollIndicator = NO;
其上面
_tableView.contentInset = UIEdgeInsetsMake(200,0, 0, 0);
設(shè)置tableview的內(nèi)容偏移來給圖片預(yù)留位置。
創(chuàng)建imageView
_topImageView = [[UIImageView alloc] init];
_topImageView.frame = CGRectMake(0, -200, [UIScreen mainScreen].bounds.size.width, 200);
_topImageView.contentMode = UIViewContentModeScaleAspectFill;
_topImageView.image = [UIImage imageNamed:@"頂部圖"];
_topImageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
_topImageView.clipsToBounds = YES;
_topImageView.tag = 101;
[_tableView addSubview:_topImageView];
設(shè)置delagate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point = scrollView.contentOffset;
if (point.y < -200) {
CGRect rect = [self.tableView viewWithTag:101].frame;
rect.origin.y = point.y;
rect.size.height = -point.y;
[self.tableView viewWithTag:101].frame = rect;
}
}
結(jié)語
要是tableView 預(yù)留的位置铭乾,跟圖片錯開的位置相同,我demo中的imageView 高 200娃循。然后圖片的contentMode 一定要是UIViewContentModeScaleAspectFill炕檩。