iOS开发-仿大众点评iPad侧边导航栏

十度 IOS 2015年12月01日 收藏

昨天其实已经写了一篇侧边栏的文章,不过感觉还不是很清晰,这篇文章算是补充吧,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. _backgroundImage=backgroundImage;
  54. [self setImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal];
  55. }
  56. //设置选中图片
  57. -(void)setSelectedImage:(NSString *)selectedImage{
  58. _selectedImage=selectedImage;
  59. [self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateDisabled];
  60. }
  61. //设置图片区域
  62. -(CGRect)imageRectForContentRect:(CGRect)contentRect{
  63. CGFloat width=contentRect.size.width;
  64. CGFloat height= contentRect.size.height * 0.7;
  65. return CGRectMake(0, 10, width, height);
  66. }
  67. //设置文字区域
  68. -(CGRect)titleRectForContentRect:(CGRect)contentRect{
  69. CGFloat width=contentRect.size.width;
  70. CGFloat height= contentRect.size.height * 0.3;
  71. CGFloat position=contentRect.size.height*0.7;
  72. return CGRectMake(0, position, width, height);
  73. }
  74.  
  75. -(void)setFrame:(CGRect)frame{
  76. //固定Item宽高
  77. frame.size=CGSizeMake(GPDockItemWidth, GPDockItemHeight);
  78. [super setFrame:frame];
  79. }
  80.  
  81. @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. [tabItem imageSetting:@"Toolbar_switchcity.png" selectedImage:@"Toolbar_switchcity_selected.png"];
  4. CGFloat y = self.frame.size.height - GPDockItemHeight*2-20;
  5. //设置位置
  6. tabItem.frame = CGRectMake(0, y, 0, 0);
  7. [tabItem setTitle:@"北京"];
  8. //设置选中触摸选中事件
  9. [tabItem addTarget:self action:@selector(tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown];
  10. tabItem.tag =4;
  11. [self addSubview:tabItem];
  12. }

 GPDock.h中的设置:

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

  两者有相同之处,分开合并都行,具体看将来要实现的业务逻辑,将其添加到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. [self addTabItems];
  10. //添加设置
  11. [self addLocation];
  12. //添加设置
  13. [self addSetting];
  14. }
  15. return self;
  16. }

 最终实现效果如下:

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