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

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

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

//
// GPDockItem.h
// GrouponProject
//博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
// Created by keso on 15/3/11.
// Copyright (c) 2015年 keso. All rights reserved.
// #import <UIKit/UIKit.h> @interface GPDockItem : UIButton -(void)imageSetting:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage; @property (nonatomic,strong) NSString *title; //背景图片
@property (nonatomic,strong) NSString *backgroundImage;
//选中图片
@property (nonatomic,strong) NSString *selectedImage; @end

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

//设置图片区域
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat width=contentRect.size.width;
CGFloat height= contentRect.size.height * 0.7;
return CGRectMake(0, 10, width, height);
}
//设置文字区域
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat width=contentRect.size.width;
CGFloat height= contentRect.size.height * 0.3;
CGFloat position=contentRect.size.height*0.7;
return CGRectMake(0, position, width, height);
}

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

-(void)imageSetting:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage{
self.backgroundImage=backgroundImage; self.selectedImage=selectedImage;
}

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

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

 GPDockItem.m中的代码:

//
// GPDockItem.m
// GrouponProject
//博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang
// Created by keso on 15/3/11.
// Copyright (c) 2015年 keso. All rights reserved.
// #import "GPDockItem.h" @implementation GPDockItem /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ -(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
// if (self) {
//// UIImageView *splitLine = [[UIImageView alloc] init];
//// splitLine.frame = CGRectMake(0, 0, GPDockItemWidth, 2);
//// splitLine.image = [UIImage imageNamed:@"separator_tabbar_item.png"];
//// [self addSubview:splitLine];
//
// }
return self; }
-(void)setTitle:(NSString *)title{
[self setTitle:title forState:UIControlStateNormal];
self.titleLabel.textAlignment=NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:15];
//正常状态下是灰色
[self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//不可点击的时候切换文字颜色
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
//设置图片属性
self.imageView.contentMode = UIViewContentModeCenter;
} -(void)imageSetting:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage{
self.backgroundImage=backgroundImage; self.selectedImage=selectedImage;
} //设置背景图片
-(void)setBackgroundImage:(NSString *)backgroundImage{ _backgroundImage=backgroundImage; [self setImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal]; }
//设置选中图片
-(void)setSelectedImage:(NSString *)selectedImage{
_selectedImage=selectedImage;
[self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateDisabled]; }
//设置图片区域
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat width=contentRect.size.width;
CGFloat height= contentRect.size.height * 0.7;
return CGRectMake(0, 10, width, height);
}
//设置文字区域
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat width=contentRect.size.width;
CGFloat height= contentRect.size.height * 0.3;
CGFloat position=contentRect.size.height*0.7;
return CGRectMake(0, position, width, height);
} -(void)setFrame:(CGRect)frame{
//固定Item宽高
frame.size=CGSizeMake(GPDockItemWidth, GPDockItemHeight);
[super setFrame:frame];
} @end

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

//
// GPBottomItem.m
// GrouponProject
// FlyElephant--http://www.cnblogs.com/xiaofeixiang
// Created by keso on 15/3/13.
// Copyright (c) 2015年 keso. All rights reserved.
// #import "GPBottomItem.h" @implementation GPBottomItem /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ -(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
// 自动伸缩
self.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;
}
return self;
} @end

GPDock.h中的定位:

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

 GPDock.h中的设置:

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

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

-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
//自动伸缩高度可伸缩,右边距可以伸缩
self.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin;
//设置背景图片
self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Toolbar_bg_tabbar.png"]]; //添加选项卡
[self addTabItems]; //添加设置
[self addLocation]; //添加设置
[self addSetting];
}
return self;
}

 最终实现效果如下:

时间匆匆,行笔仓促,难免遗漏,欢迎指正,写博不易,如有好感,请尽情推荐,最近需要换工作,有相关的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. ubuntu VNC中Xfce4中Tab键失效的解决方法

    转:https://blog.csdn.net/xuezhisdc/article/details/48662435 说明 在Ubuntu Server 14.04上安装了xfce4桌面环境,但是却发 ...

  2. python 字典相关操作

    字典 字典的增删改查 字典的创建方式: # 创建字典类型 info = { 'name':'李白', 'age':'25', 'sex':'男' } msg = { 'user01':'Longzel ...

  3. [Java]jdbc[转]

    >>http://www.cnblogs.com/xiohao/p/3507483.html >>http://www.cnblogs.com/hongten/archive/ ...

  4. 在mysql中使用group by和order by取每个分组中日期最大一行数据

    转载自:https://blog.csdn.net/shiyong1949/article/details/78482737 在mysql中使用group by进行分组后取某一列的最大值,我们可以直接 ...

  5. Linux学习笔记07—mysql的配置

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  6. 老菜鸟致青春,程序员应该选择java 还是 c#-

    致青春 还记得自己那年考清华失败,被调剂到中科大软院,当初有几个方向可以选,软件设计.嵌入式.信息安全等等,毫不犹豫地选择了信息安全. 为什么选信息安全?这四个字听起来多牛多有感觉,我本科是学物理的, ...

  7. Visual Studio新的 .csporj 文件

    Visual Studio新的 .csporj 文件非常方便,虽然目前还不支持WPF.WinForm等工程,但应用到控制台程序,类库还是没有任何问题的.只需要简单的用如下内容替换老的csproj即可: ...

  8. go中的接口

    对于golang的接口,纠结两天了,今天有种茅塞顿开的感觉,有必要写点东西了. 纠结接口,说白了就是搞不透接口,方法,结构体几者之间的关系以及具体的用途.可以简单的从三者的定义说起,接口说白了就是一个 ...

  9. C#远程调用技术WebService修炼手册

    一.课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上.所以在本次分享课开课之 ...

  10. delphi 服务程序

    http://www.delphifans.com/InfoView/Article_662.html 用Delphi创建服务程序 Windows 2000/XP和2003等支持一种叫做"服 ...