昨天其实已经写了一篇侧边栏的文章,不过感觉还不是很清晰,这篇文章算是补充吧,iPad上看了大众点评的侧边栏,基本上百分之九十类似,具体效果可参考下图:

对比昨天主要做了两个修改,一个是图片和文字的显示位置,另外一个就是关于底部的定位和设置的位置在横竖屏时显示的问题,侧边栏的区域是是自己控制的,需要注意一下横竖屏的时候设置一下autoresizingMask,底部图标定位的时候也是一样设置。

导航栏上每个按钮提取出了一个父类GPDockItem,头文件中的代码:

  1. //
  2. // GPDockItem.h
  3. // GrouponProject
  4. //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/11.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface GPDockItem : UIButton
  12.  
  13. -(void)imageSetting:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage;
  14.  
  15. @property (nonatomic,strong) NSString *title;
  16.  
  17. //背景图片
  18. @property (nonatomic,strong) NSString *backgroundImage;
  19. //选中图片
  20. @property (nonatomic,strong) NSString *selectedImage;
  21.  
  22. @end

相对于之前的代码,主要是添加了设置背景图片和设置选中图片的混合方法,定义了一个Title属性,之后的可以设置文字和图片的位置,重写两个方法:

  1. //设置图片区域
  2. -(CGRect)imageRectForContentRect:(CGRect)contentRect{
  3. CGFloat width=contentRect.size.width;
  4. CGFloat height= contentRect.size.height * 0.7;
  5. return CGRectMake(0, 10, width, height);
  6. }
  7. //设置文字区域
  8. -(CGRect)titleRectForContentRect:(CGRect)contentRect{
  9. CGFloat width=contentRect.size.width;
  10. CGFloat height= contentRect.size.height * 0.3;
  11. CGFloat position=contentRect.size.height*0.7;
  12. return CGRectMake(0, position, width, height);
  13. }

 设置背景图片和选中图片:

  1. -(void)imageSetting:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage{
  2. self.backgroundImage=backgroundImage;
  3.  
  4. self.selectedImage=selectedImage;
  5. }

 设置显示文字和图片在区域内的位置:

  1. -(void)setTitle:(NSString *)title{
  2. [self setTitle:title forState:UIControlStateNormal];
  3. self.titleLabel.textAlignment=NSTextAlignmentCenter;
  4. self.titleLabel.font = [UIFont systemFontOfSize:15];
  5. //正常状态下是灰色
  6. [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  7. //不可点击的时候切换文字颜色
  8. [self setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
  9. //设置图片属性
  10. self.imageView.contentMode = UIViewContentModeCenter;
  11. }

 GPDockItem.m中的代码:

  1. //
  2. // GPDockItem.m
  3. // GrouponProject
  4. //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/11.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import "GPDockItem.h"
  10.  
  11. @implementation GPDockItem
  12.  
  13. /*
  14. // Only override drawRect: if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. - (void)drawRect:(CGRect)rect {
  17. // Drawing code
  18. }
  19. */
  20.  
  21. -(instancetype)initWithFrame:(CGRect)frame{
  22. self=[super initWithFrame:frame];
  23. // if (self) {
  24. //// UIImageView *splitLine = [[UIImageView alloc] init];
  25. //// splitLine.frame = CGRectMake(0, 0, GPDockItemWidth, 2);
  26. //// splitLine.image = [UIImage imageNamed:@"separator_tabbar_item.png"];
  27. //// [self addSubview:splitLine];
  28. //
  29. // }
  30. return self;
  31.  
  32. }
  33. -(void)setTitle:(NSString *)title{
  34. [self setTitle:title forState:UIControlStateNormal];
  35. self.titleLabel.textAlignment=NSTextAlignmentCenter;
  36. self.titleLabel.font = [UIFont systemFontOfSize:15];
  37. //正常状态下是灰色
  38. [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  39. //不可点击的时候切换文字颜色
  40. [self setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
  41. //设置图片属性
  42. self.imageView.contentMode = UIViewContentModeCenter;
  43. }
  44.  
  45. -(void)imageSetting:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage{
  46. self.backgroundImage=backgroundImage;
  47.  
  48. self.selectedImage=selectedImage;
  49. }
  50.  
  51. //设置背景图片
  52. -(void)setBackgroundImage:(NSString *)backgroundImage{
  53.  
  54. _backgroundImage=backgroundImage;
  55.  
  56. [self setImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal];
  57.  
  58. }
  59. //设置选中图片
  60. -(void)setSelectedImage:(NSString *)selectedImage{
  61. _selectedImage=selectedImage;
  62. [self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateDisabled];
  63.  
  64. }
  65. //设置图片区域
  66. -(CGRect)imageRectForContentRect:(CGRect)contentRect{
  67. CGFloat width=contentRect.size.width;
  68. CGFloat height= contentRect.size.height * 0.7;
  69. return CGRectMake(0, 10, width, height);
  70. }
  71. //设置文字区域
  72. -(CGRect)titleRectForContentRect:(CGRect)contentRect{
  73. CGFloat width=contentRect.size.width;
  74. CGFloat height= contentRect.size.height * 0.3;
  75. CGFloat position=contentRect.size.height*0.7;
  76. return CGRectMake(0, position, width, height);
  77. }
  78.  
  79. -(void)setFrame:(CGRect)frame{
  80. //固定Item宽高
  81. frame.size=CGSizeMake(GPDockItemWidth, GPDockItemHeight);
  82. [super setFrame:frame];
  83. }
  84.  
  85. @end

继承自GPDockItem的GPBottomItem,只需要设置横竖屏自动伸缩属性即可:

  1. //
  2. // GPBottomItem.m
  3. // GrouponProject
  4. // FlyElephant--http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/3/13.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import "GPBottomItem.h"
  10.  
  11. @implementation GPBottomItem
  12.  
  13. /*
  14. // Only override drawRect: if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. - (void)drawRect:(CGRect)rect {
  17. // Drawing code
  18. }
  19. */
  20.  
  21. -(instancetype)initWithFrame:(CGRect)frame{
  22. self=[super initWithFrame:frame];
  23. if (self) {
  24. // 自动伸缩
  25. self.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;
  26. }
  27. return self;
  28. }
  29.  
  30. @end

GPDock.h中的定位:

  1. -(void)addLocation{
  2. GPBottomItem *tabItem=[[GPBottomItem alloc]init];
  3.  
  4. [tabItem imageSetting:@"Toolbar_switchcity.png" selectedImage:@"Toolbar_switchcity_selected.png"];
  5.  
  6. CGFloat y = self.frame.size.height - GPDockItemHeight*2-20;
  7. //设置位置
  8. tabItem.frame = CGRectMake(0, y, 0, 0);
  9.  
  10. [tabItem setTitle:@"北京"];
  11.  
  12. //设置选中触摸选中事件
  13. [tabItem addTarget:self action:@selector(tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown];
  14. tabItem.tag =4;
  15. [self addSubview:tabItem];
  16. }

 GPDock.h中的设置:

  1. -(void)addSetting{
  2. GPBottomItem *tabItem=[[GPBottomItem alloc]init];
  3.  
  4. [tabItem imageSetting:@"Toolbar_setting.png" selectedImage:@"Toolbar_setting_selected.png"];
  5.  
  6. CGFloat y = self.frame.size.height - GPDockItemHeight-10;
  7. //设置位置
  8. tabItem.frame = CGRectMake(0, y, 0, 0);
  9.  
  10. [tabItem setTitle:@"设置"];
  11. //设置选中触摸选中事件
  12. [tabItem addTarget:self action:@selector(tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown];
  13. tabItem.tag =5;
  14. [self addSubview:tabItem];
  15. }

  两者有相同之处,分开合并都行,具体看将来要实现的业务逻辑,将其添加到GPDock中:

  1. -(instancetype)initWithFrame:(CGRect)frame{
  2. self=[super initWithFrame:frame];
  3. if (self) {
  4. //自动伸缩高度可伸缩,右边距可以伸缩
  5. self.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin;
  6. //设置背景图片
  7. self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Toolbar_bg_tabbar.png"]];
  8.  
  9. //添加选项卡
  10. [self addTabItems];
  11.  
  12. //添加设置
  13. [self addLocation];
  14.  
  15. //添加设置
  16. [self addSetting];
  17. }
  18. return self;
  19. }

 最终实现效果如下:

时间匆匆,行笔仓促,难免遗漏,欢迎指正,写博不易,如有好感,请尽情推荐,最近需要换工作,有相关的iOS岗位的可以提供下,先行谢过~

iOS开发-仿大众点评iPad侧边导航栏的更多相关文章

  1. iOS开发笔记13:顶部标签式导航栏及下拉分类菜单

    当内容及分类较多时,往往采用顶部标签式导航栏,例如网易新闻客户端的顶部分类导航,最近刚好有这样的应用场景,参考网络上一些demo,实现了这种导航效果,记录一些要点. 效果图(由于视频转GIF掉帧,滑动 ...

  2. iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条

    网上查了很多, 都是重新绘制, 感觉有点蠢, 恰巧工作有会闲, 就简单的通过runtime遍历了下属性找寻了下私有类和方法, 这里直接贴方法, 找寻过程也发出来, 能看懂的直接就能看懂, 看不太明白的 ...

  3. mpvue从一无所有开始仿大众点评小程序

    最近尝试了下用mpvue框架开发小程序,它是基于vue开发的. 官方介绍: mpvue 是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了 Vue.js ...

  4. iOS开发UI篇—多控制器和导航控制器简单介绍

    iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...

  5. iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期

    iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期 一.基本过程 新建一个项目,系统默认的主控制器继承自UIViewController,把主控制器两个文件删掉. 在stor ...

  6. iOS开发UI篇—模仿ipad版QQ空间登录界面

    iOS开发UI篇—模仿ipad版QQ空间登录界面 一.实现和步骤 1.一般ipad项目在命名的时候可以加一个HD,标明为高清版 2.设置项目的文件结构,分为home和login两个部分 3.登陆界面的 ...

  7. [置顶] bootstrap自定义样式-bootstrap侧边导航栏的实现

    前言 bootstrap自带的响应式导航栏是向下滑动的,有时满足不了个性化的需求,需要做一个类似于android drawerLayout 侧滑的菜单,这就是我要实现的bootstrap自定义侧滑菜单 ...

  8. Android 新兴的UI模式——侧边导航栏【转】

    侧边导航栏也就是大家熟知的SliddingMenu,英文也叫Fly-In App Menu.Side Navigation等.当然谷歌现在已经推出类似这个效果的组件--Navigation Drawe ...

  9. iOS之旅--隐藏(去除)导航栏底部横线

    iOS之旅--隐藏(去除)导航栏底部横线 iOS开发大部分情况下会使用到导航栏,由于我司的app导航栏需要与下面紧挨着的窗口颜色一致,导航栏底部的横线就会影响这个美观,LZ使用了以下方法.觉得不错,分 ...

随机推荐

  1. nginx命令大全

    sudo nginx #打开 nginx nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx nginx -t #测试配置是否有语法错误 n ...

  2. curl请求指定host ip(指定域名解析的内网某ip)

    域名www.test.com解析内部多台ip $httpHeader = array('Host: www.test.com');$url = "10.17.2.245/xxx/xxx/t. ...

  3. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学

    C. Ray Tracing 题目连接: http://codeforces.com/contest/724/problem/C Description oThere are k sensors lo ...

  4. oracle统计字符串包含字符个数

    函数:REGEXP_COUNT(); select REGEXP_COUNT('1,2,6,8,7,9',',') from dual 结果:5

  5. 青客宝团队Consul内部分享ppt

    青客宝团队Consul内部分享ppt   https://mp.weixin.qq.com/s?src=3&timestamp=1503647705&ver=1&signatu ...

  6. Booting LPC-Link2, Updating LPCXpresso firmware

    Booting LPC-Link2 The recommended way to use LPC-Link2 with the LPCXpresso IDE is to boot and soft l ...

  7. [Go] 反射 - reflect.ValueOf()

    类型 和 接口 由于反射是基于类型系统(type system)的,所以先简单了解一下类型系统. 首先 Golang 是一种静态类型的语言,在编译时每一个变量都有一个类型对应,例如:int, floa ...

  8. Cocos2d-x3.0下实现循环列表

    本文的实现是參照我之前在做iOS时实现的一个能够循环的列表这里用C++重写一遍. 效果: 原文地址:http://blog.csdn.net/qqmcy/article/details/2739301 ...

  9. 【Elasticsearch】ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]];

    ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC ...

  10. Mysql select语句设置默认值

    1.在没有设置默认值的情况下: SELECT userinfo.id, user_name, role, adm_regionid, region_name , create_time FROM us ...