iOS 悬停 你值得一看

导航条隐藏
显示导航条

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

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,515评论 1 14
  • 关爱老人不光是要他们吃好,穿好,要有一定的物质生活以外,更要有好的交流,沟通,这是潍坊惟爱公益助残服务中心的公益人...
    随心姐阅读 547评论 3 2
  • 其实我也写作也坚持了有几年,我一直有写博客的习惯,但我有一梦想就像当一个作家,但每次和父母说你“”不要做白日梦了。...
    cca8645a5515阅读 479评论 0 0