iOS开发-仿大众点评iPad侧边导航栏
昨天其实已经写了一篇侧边栏的文章,不过感觉还不是很清晰,这篇文章算是补充吧,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侧边导航栏的更多相关文章
- iOS开发笔记13:顶部标签式导航栏及下拉分类菜单
当内容及分类较多时,往往采用顶部标签式导航栏,例如网易新闻客户端的顶部分类导航,最近刚好有这样的应用场景,参考网络上一些demo,实现了这种导航效果,记录一些要点. 效果图(由于视频转GIF掉帧,滑动 ...
- iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条
网上查了很多, 都是重新绘制, 感觉有点蠢, 恰巧工作有会闲, 就简单的通过runtime遍历了下属性找寻了下私有类和方法, 这里直接贴方法, 找寻过程也发出来, 能看懂的直接就能看懂, 看不太明白的 ...
- mpvue从一无所有开始仿大众点评小程序
最近尝试了下用mpvue框架开发小程序,它是基于vue开发的. 官方介绍: mpvue 是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了 Vue.js ...
- iOS开发UI篇—多控制器和导航控制器简单介绍
iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...
- iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期
iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期 一.基本过程 新建一个项目,系统默认的主控制器继承自UIViewController,把主控制器两个文件删掉. 在stor ...
- iOS开发UI篇—模仿ipad版QQ空间登录界面
iOS开发UI篇—模仿ipad版QQ空间登录界面 一.实现和步骤 1.一般ipad项目在命名的时候可以加一个HD,标明为高清版 2.设置项目的文件结构,分为home和login两个部分 3.登陆界面的 ...
- [置顶]
bootstrap自定义样式-bootstrap侧边导航栏的实现
前言 bootstrap自带的响应式导航栏是向下滑动的,有时满足不了个性化的需求,需要做一个类似于android drawerLayout 侧滑的菜单,这就是我要实现的bootstrap自定义侧滑菜单 ...
- Android 新兴的UI模式——侧边导航栏【转】
侧边导航栏也就是大家熟知的SliddingMenu,英文也叫Fly-In App Menu.Side Navigation等.当然谷歌现在已经推出类似这个效果的组件--Navigation Drawe ...
- iOS之旅--隐藏(去除)导航栏底部横线
iOS之旅--隐藏(去除)导航栏底部横线 iOS开发大部分情况下会使用到导航栏,由于我司的app导航栏需要与下面紧挨着的窗口颜色一致,导航栏底部的横线就会影响这个美观,LZ使用了以下方法.觉得不错,分 ...
随机推荐
- Xamarin iOS教程之申请付费开发者账号下载证书
Xamarin iOS教程之申请付费开发者账号下载证书 Xamarin iOS使用真机测试应用程序 在讲解iOS Simulator时,已经提到了虽然iOS Simulator可以模仿真实的设备,但是 ...
- dsu on tree题表
dsu on tree,又名树上启发式合并.重链剖分,是一类十分实用的trick,它常常可以作为一些正解的替代算法: 1.DFS序+线段树/主席树/线段树合并 2.对DFS序分块的树上莫队 3.长链剖 ...
- 【NOI2005】聪聪和可可 概率与期望 记忆化搜索
1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1635 Solved: 958[Submit][Statu ...
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror(转)
dlopen 基本定义 功能:打开一个动态链接库 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * pathn ...
- LPC43xx SGPIO Configuration -- Why not use GPDMA ?
LPC43xx SGPIO Configuration The LPC43xx SGPIO peripheral is used to move samples between USB and the ...
- VGA Signal Timing
VGA Signal Timing 640 x 350 VGA 640x350@70 Hz (pixel clock 25.175 MHz) VESA 640x350@85 Hz (pixel clo ...
- Java 微服务实践 - Spring Boot 系列
https://segmentfault.com/l/1500000009515571
- [Asp.net mvc]国际化
摘要 在实际项目中,经常遇到,开发的项目要提供给不同的国家使用,如果根据国家来开发不同的站点,肯定是非常耗时又耗成本的.asp.net中,提供了一种比较方便的方式,可以使用资源文件的方式,使我们的站点 ...
- 在Delphi中操作快捷方式
快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序.文件和文件夹非常有用.在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头 ...
- ListBox使用
一.什么是ListBox? ListBox 是一个显示项集合的控件.一次可以显示 ListBox 中的多个项. ListBox继承自ItemsControl,可以使用Items或者ItemsSourc ...