話不多說,我們直接上代碼吧:
添加兩個(gè)屬性
//UIScrollView
@property (nonatomic, strong) UIScrollView *scView;
//懸停View
@property (nonatomic, strong) UIView *topView;
//不要忘記添加 UIScrollView的協(xié)議
//存在導(dǎo)航條時(shí) 將導(dǎo)航條隱藏
self.navigationController.navigationBar.hidden = YES;
//存在導(dǎo)航條時(shí)
_scView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//不存在導(dǎo)航條時(shí)
//? ? _scView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
[self.view addSubview:_scView];
_scView.backgroundColor = [UIColor redColor];
_scView.contentSize = CGSizeMake(self.view.frame.size.width, 1000);
//創(chuàng)建一個(gè)button
UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
[_scView addSubview:but];
but.backgroundColor = [UIColor greenColor];
//創(chuàng)建頂部的條
_topView = [[UIView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 50)];
_topView.backgroundColor = [UIColor greenColor];
[_scView addSubview:_topView];
self.scView.delegate = self;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (self.scView.contentOffset.y > 200) {
//不存在導(dǎo)航條時(shí)
//self.topView.frame = CGRectMake(0, 64, self.view.frame.size.width, 50) ;
//存在導(dǎo)航條時(shí)
self.topView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50) ;
//添加的視圖 不同
[self.view addSubview:self.topView];
}else
{self.topView.frame = CGRectMake(0, 200, self.view.frame.size.width, 50) ;
[self.scView addSubview:self.topView];
}
根據(jù)偏移量的多少 來判斷View的移動(dòng)
+重點(diǎn)+懸停后所填加的視圖為主視圖
+++++中心? 順手點(diǎn)個(gè)贊