iOS下简单实现滑动导航条
功能介绍
最近在做一款ios的app,其中有一个页面需要分成三个版块,版块之间可以通过左右滑动来进行切换,也可以通过点击上方的按钮来切换,好像在android中可以用过ViewPager + Fragment来实现。界面大概就是这个样子,这里我以知乎客户端的发现页面为例:

其中整个页面分为三个小的版块,分别"推荐","热门"以及"收藏"。点击上方的文字后,下方的页面会切换到对应的版块,同时文字下方的蓝色导航条也会随着界面的切换跟着滑动。另外,当我们不通过按钮切换,用手势在页面上左右滑动时,导航条也会跟着一起滑动。功能大概就是这个样子。
原理
下面简单分析下实现原理。首先在"发现"的下方应该有UIView,在UIView中放有3个UIButton。至于文字下方的导航条,可以用UILabel来做,宽度等于一个按钮的宽度,并将背景色改为蓝色。导航条下方的页面可以通过手势来左右滑动,首先想到的应该就是,这是一个UIScrollView,我们需要将scrollView的contentSize属性设置成三个页面宽度的大小,否则会无法滑动页面。然后在scrollView放上三个版块的页面,这个根据自身的业务情况而定,一般UITableView用得比较多。最后,怎样才能让页面滑动的时候导航条也跟着移动的?在Android中,一般的做法就是给scrollView中添加一个事件监听器,用来监听滑动事件,并在事件处理函数中执行响应的逻辑。在ios中没有事件监听这个概念,我们可以为scrollView设置它的代理,并在代理类中覆盖掉该方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
设置了代理类后,代理类其实就是帮我们注册了相应的回调函数,每次scrollView只要有细微的滑动,该方法就会被调用。在方法中,需要做的就是获取scrollView当前的偏移量,然后根据这个偏移量来设定导航条的位置。
代码实现
将scrollView与滚动条声明未全局变量,因为在其他方法中也会用到。
//存放三个按钮的父页面宽度
#define TOP_VIEW_HEIGHT 50
//界面上方导航线条的宽度
#define NAVIGATION_LINE_HEIGHT 2
@interface MyMesgViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *navLabel;
@end
scrollView延迟初始化,在初始化时记得为其设置代理对象。
- (UIScrollView *)scrollView {
if (!_scrollView) {
CGFloat scrollViewHeight = 64 + TOP_VIEW_HEIGHT + 8;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollViewHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - scrollViewHeight)];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
CGSize size = _scrollView.frame.size;
size.width *= 3;
_scrollView.contentSize = size;
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
}
return _scrollView;
}
navLabel延迟初始化
- (UILabel *)navLabel {
if (!_navLabel) {
_navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TOP_VIEW_HEIGHT - NAVIGATION_LINE_HEIGHT, [UIScreen mainScreen].bounds.size.width / 3, NAVIGATION_LINE_HEIGHT)];
_navLabel.backgroundColor = [UIColor blueColor];
}
return _navLabel;
}
将三个按钮添加到页面中,并为按钮添加点击事件处理函数。
- (void)addMessageCategoryButton {
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, TOP_VIEW_HEIGHT)];
topView.backgroundColor = [UIColor whiteColor];
UIButton *approvedMsgButton = [self createMsgButton:@"审核消息" posX:0];
UIButton *mosMsgButton = [self createMsgButton:@"M币交易" posX:approvedMsgButton.frame.size.width];
UIButton *systemMsgButton = [self createMsgButton:@"系统通知" posX:approvedMsgButton.frame.size.width * 2];
[topView addSubview:approvedMsgButton];
[topView addSubview:mosMsgButton];
[topView addSubview:systemMsgButton];
[topView addSubview:self.navLabel];
[self.view addSubview:topView];
}
将创建按钮的过程单独抽象出来
- (UIButton *)createMsgButton:(NSString *)title posX:(CGFloat)posX {
UIButton *msgButton = [[UIButton alloc] initWithFrame:CGRectMake(posX, 0, self.view.frame.size.width / 3, TOP_VIEW_HEIGHT - NAVIGATION_LINE_HEIGHT)];
msgButton.backgroundColor = [UIColor whiteColor];
NSAttributedString *attributeTitle = [[NSAttributedString alloc] initWithString:title attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14] }];
[msgButton setAttributedTitle:attributeTitle forState:UIControlStateNormal];
msgButton.tag = (NSInteger) (posX / (self.view.frame.size.width / 3));
[msgButton addTarget:self action:@selector(switchMessageDetailView:) forControlEvents:UIControlEventTouchUpInside];
return msgButton;
}
按钮点击后调用的函数,即切换页面
- (void)switchMessageDetailView:(UIButton *)btn {
[self.scrollView setContentOffset:CGPointMake(btn.tag * self.view.frame.size.width, 0) animated:YES];
}
最后,在回调函数中根据scrollView的偏移量调整导航条的位置。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
CGFloat x = offset.x / 3;
if (x > 0 && x < scrollView.frame.size.width) {
CGRect frame = self.navLabel.frame;
frame.origin.x = x;
self.navLabel.frame = frame;
}
}
这是最终的效果图:
iOS下简单实现滑动导航条的更多相关文章
- 基于jQuery鼠标悬停上下滑动导航条
基于jQuery鼠标悬停上下滑动导航条.这是一款蓝色好看的鼠标响应式网站导航菜单特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div id="menu2& ...
- 【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转
http://blog.csdn.net/weisubao/article/details/39646739?utm_source=tuicool&utm_medium=referral (1 ...
- iOS仿今日头条滑动导航
之前写了篇博客网易首页导航封装类.网易首页导航封装类优化,今天在前两个的基础上仿下今日头条. 1.网易首页导航封装类中主要解决了上面导航的ScrollView和下面的页面的ScrollView联动的问 ...
- 【Andord真】SlideMenu+ViewPagerIndictor双滑动边栏+滑动导航条
采取SlideMenu达到的效果侧边栏: 间 setContentView是设置主背景的布局 setBehindContentView是设置左边菜单的布局 setSecondaryMenu是设置右边的 ...
- 【原】Github系列之三:开源iOS下 渐变颜色的进度条WGradientProgress
概述 今天我们来实现一个iOS平台上的进度条(progress bar or progress view).这种进度条比APPLE自带的更加漂亮,更加有“B格”.它拥有渐变的颜色,而且这种颜色是动态移 ...
- Bootstrap学习笔记(5)--实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单
实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单 微笑的鱼 2014-01-03 Bootstrap 5,281 次围观 11条评论 使用Bootstrap导航条组件时,如果你的导航条带有下拉 ...
- 【转】一个DIV+CSS代码布局的简单导航条
原文地址:http://www.divcss5.com/shili/s731.shtml 简单的DIV CSS代码布局实现导航条 一个蓝色主题的导航条布局案例,本CSS小实例,采用DIV CSS实现. ...
- ch6 列表和导航条
为列表添加定制的项目符号 可使用list-style-image属性:缺点是对项目符号图像的位置的控制能力不强. 常用的方法:使用list-style-type来关闭项目符号,将定制的项目符号作为背景 ...
- bootstrap 组件之"导航条"
一个典型的导航条基本代码如下: <nav class="navbar navbar-default"> <div class="container&qu ...
随机推荐
- Show Desktop Pro FAQ
Q. Will the desktop background image be restored after quit? A: Yes. Right now, "Hide icons&quo ...
- Hibernate的懒加载session丢失解决方法
在web.xml加入spring提供的过滤器,延长session的生命周期 <!--Hibernate的懒加载session丢失解决方法 --> <filter> <fi ...
- python-打印进度条
progress_bar.py #!/usr/bin/python3.6 #__*__uft8__*__ import sys import time def progress(percent,wid ...
- 使用vs的时候,遇到这个:当前不会命中断点 还没有为该文档加载任何符号
一 http://stackoverflow.com/questions/2155930/fixing-the-breakpoint-will-not-currently-be-hit-no-symb ...
- 推荐一个JavaScript触发器插件,可通过指定频次、指定时间内触发指定的处理函数
推荐一个JavaScript触发器插件js-trigger js-trigger是一个JavaScript触发器插件,可通过指定频次.指定时间内触发指定的处理函数 https://tanwei-cc. ...
- 各类人工智能&大数据相关比赛
比赛技巧:https://zhuanlan.zhihu.com/p/28084438 文章来源: https://www.imooc.com/article/72863 随着近几年人工智能和大数据的快 ...
- Glibc-2.3.4编译
$tar xf Glibc2.3.4.tar.bz2 $mkdir build_glibc $cd build_glibc ../glibc-2.3.4/configure --prefix=/too ...
- Git 设置 SOCKS 代理
$ export all_proxy=socks5://127.0.0.1:1080
- Linux虚拟机忘记root密码的拯救办法
于是百度之,还是有很多解决方案的.其原理就是,进入到single单用户模式下,去修改root密码: 1.重启系统,选择Linux引导,然后按住e,出现如下界面: 按下e 出现如下页面 选择kernel ...
- [Tomcat 部署问题] Undeployment Failure could not be redeployed ...
Tomcat 部署,在部署可能会出现以下问题: Deployment failure on Tomcat 6.x. Could not copy all resources to E:\apache- ...