美文网首页
iOS 悬停 你值得一看

iOS 悬停 你值得一看

作者: TikBai | 来源:发表于2017-05-16 20:16 被阅读0次
导航条隐藏
显示导航条

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

添加两个属性
//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的移动
+重点+悬停后所填加的视图为主视图

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

相关文章

网友评论

      本文标题:iOS 悬停 你值得一看

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