
屏幕快照 2018-09-21 下午5.07.19.png

屏幕快照 2018-09-21 下午5.08.16.png
- 進(jìn)入頁面隱藏導(dǎo)航欄 退出頁面顯示導(dǎo)航欄
- (void)viewWillAppear:(BOOL)animated {
self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
self.navigationController.navigationBar.hidden = NO;
}
2.依次創(chuàng)建tableView, headImg, navView
tableView要設(shè)置contentInset,top值為圖片高度-20
static const CGFloat kHeadImgWidth = 375;
static const CGFloat kHeadImgHeight = 165;
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
CGFloat headImvH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
_tableView.contentInset = UIEdgeInsetsMake(headImvH-20, 0, 0, 0);
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
- (UIImageView *)headImv {
if (!_headImg) {
_headImg = [[UIImageView alloc] init];
CGFloat headImvH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
_headImg.frame = CGRectMake(0, 0, SCREEN_WIDTH, headImvH);
_headImg.image = [UIImage imageNamed:@"login_bg"];
_headImg.contentMode = UIViewContentModeScaleAspectFill;
_headImg.clipsToBounds = YES;
}
return _headImg;
}
- (UIView *)navView {
if (!_navView) {
_navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
_navView.backgroundColor = CellWordDarkColor;
_navView.clipsToBounds = YES;
_titLab = [[UILabel alloc]init];
_titLab.centerX = SCREEN_WIDTH * 0.5;
_titLab.bounds = CGRectMake(0, 0, 150, 44);
_titLab.textAlignment = NSTextAlignmentCenter;
_titLab.font = [UIFont systemFontOfSize:12];
_titLab.textColor = [UIColor whiteColor];
_titLab.numberOfLines = 0;
_titLab.text = @"明天休息了";
[_navView addSubview:_titLab];
}
return _navView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = GlobalBGColor;
[self.view addSubview:self.tableView];
[self.view addSubview:self.headImv];
[self.view addSubview:self.navView];
}
- 滾動時調(diào)用scrollView的代理方法(重要)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsety = scrollView.contentOffset.y;
// 按比例換算出頭像的初始化高度
NSInteger headerH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
// 拉伸后的高度 = 實際高度+偏移量
CGFloat offset = headerH + offsety;
// 圖片移出可視范圍,導(dǎo)航欄逐漸顯示;圖片進(jìn)入視線,逐漸隱藏
// 標(biāo)題的高度亦隨之變化
self.navView.alpha = offset / 180;
if (self.navView.alpha >= 1) {
self.navView.alpha = 1;
}
self.titLab.Y = 64 - 44 * self.navView.alpha;
// 圖片的高度隨其他內(nèi)容顯隱而隨之變化
self.headImv.frame = CGRectMake(0, 0, SCREEN_WIDTH, headerH - offset);
}
- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"測試數(shù)據(jù)——%td",indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"測試數(shù)據(jù)——%td",indexPath.row];
return cell;
}