iOS 悬停 你值得一看

2017-05-16  本文已影响0人  TikBai
导航条隐藏
显示导航条

话不多说,我们直接上代码吧:

添加两个属性
//UIScrollView
@property (nonatomic, strong) UIScrollView *scView;
//悬停View
@property (nonatomic, strong) UIView *topView;
//不要忘记添加 UIScrollView的协议


//存在导航条时 将导航条隐藏
self.navigationController.navigationBar.hidden = YES;
//存在导航条时
_scView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//不存在导航条时
//    _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);
//创建一个button
UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
[_scView addSubview:but];
but.backgroundColor = [UIColor greenColor];
//创建顶部的条
_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) {
//不存在导航条时
//self.topView.frame = CGRectMake(0, 64, self.view.frame.size.width, 50) ;
//存在导航条时
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];
}



根据偏移量的多少 来判断View的移动
+重点+悬停后所填加的视图为主视图

+++++中心  顺手点个赞

上一篇 下一篇

猜你喜欢

热点阅读