iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)
iOS学习(UI)知识点整理
一、自定义标签栏
1、方法一 单个创建标签栏
#import "AppDelegate.h"
#import "SecondViewController.h"
#import "ViewController.h"
#import "ThirdViewController.h"
#import "ForthViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
@interface AppDelegate ()<UITabBarControllerDelegate>
@end @implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.直接设置默认的标签的属性
UINavigationController *navi1 = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
//设置navi1所对应的界面的标签的标题
navi1.tabBarItem.title = @"home";
//设置navi1所对应的标签的图标
navi1.tabBarItem.image = [[UIImage imageNamed:@"tabbar_account_press"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//2.直接创建一个新的标签
UIViewController *vc1 = [ViewController1 new];
//创建的方式里面包含三个参数:文字标题,普通状态下的图片,选中状态下的图片
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"界面二" image:[UIImage imageNamed:@"tabbar_appfree"]
selectedImage:[UIImage imageNamed:@"tabbar_account"]];
vc1.tabBarItem = item;
//设置数字徽标,用来提示用户
item.badgeValue = @"";
//设置系统图标右上角的数字
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:]; //3.创建标签的另一种方式
UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:[ViewController2 new]];
//参数:标题和图片
UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"界面三" image:[UIImage imageNamed:@"tabbar_reduceprice"] tag:];
item2.selectedImage = [UIImage imageNamed:@"tabbar_subject"];
navi2.tabBarItem = item2; //4.他们系统样式的标签
UINavigationController *navi3 = [[UINavigationController alloc]initWithRootViewController:[ViewController3 new]];
//使用系统的样式创建标签,图片和文字都无法修改
navi3.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:];
//无法修改
navi3.tabBarItem.title = @"界面四";
UINavigationController *navi4 = [[UINavigationController alloc]initWithRootViewController:[ViewController4 new]];
navi4.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:]; //如果创建的标签数量大于5个,则从第5个开始(包括第5个)都会被放到More标签中
UINavigationController *navi5 = [[UINavigationController alloc]initWithRootViewController:[SecondViewController new]];
navi5.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:]; //创建标签栏控制器
UITabBarController *tabbar = [[UITabBarController alloc]init];
//设置标签栏控制器所管理的视图控制器
tabbar.viewControllers = @[navi1,vc1,navi2,navi3,navi4,navi5];
NSInteger index = [[[NSUserDefaults standardUserDefaults]valueForKey:@"selectedindex"] integerValue];
//设置tabbar选中的标签
tabbar.selectedIndex = index;
tabbar.delegate = self;
self.window.rootViewController = tabbar;
self.window.backgroundColor = [UIColor whiteColor];
return YES;
} //选中某一个视图控制器的时候,调用该方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[[NSUserDefaults standardUserDefaults]setValue:@(tabBarController.selectedIndex) forKey:@"selectedindex"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(@"%@",viewController);
} //自定义视图控制器完成的时候调用
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers
changed:(BOOL)changed
{
NSLog(@"%@",viewControllers);
}
@end
2、方法二 循环遍历创建标签栏
1)创建一个继承自UIButton的类 MyTabbBarItem 用于创建标签栏的按钮
MyTabbBarItem.h 文件中的代码实现
#import <UIKit/UIKit.h>
@interface MyTabbBarItem : UIButton
@end
2) MyTabbBarItem.m 文件中的代码实现
#import "MyTabbBarItem.h"
@implementation MyTabbBarItem
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.titleLabel.font = [UIFont systemFontOfSize:];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
}
return self;
} //这个方法返回的cgrect是按钮上的title部分的位置和大小
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
return CGRectMake(, , contentRect.size.width, );
} - (CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake((contentRect.size.width - )/, , , );
}
3)自定义标签栏的类 MyTabBarController.h 代码实现
#import <UIKit/UIKit.h>
@interface MyTabBarController : UITabBarController @end
4)自定义标签栏的类 MyTabBarController.m 代码实现
#import "MyTabBarController.h"
#import "ViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "MyTabbBarItem.h" @interface MyTabBarController ()
{
UIImageView *_myTabbar;
}
@end @implementation MyTabBarController - (void)viewDidLoad {
[super viewDidLoad]; //配置标签栏控制器
//自定义标签栏的步骤 //1.隐藏系统的标签栏
self.tabBar.hidden = YES; //3.创建所有的视图控制器
[self createViewControllers]; //2.创建一个新标签栏
[self createTabbar]; //4.创建所有标签
[self createTabs]; //5.标签和视图控制器进行关联 } -(void)createTabbar
{
CGRect frame = [[UIScreen mainScreen]bounds];
frame.origin.y = frame.size.height - ;
frame.size.height = ; _myTabbar = [[UIImageView alloc]initWithFrame:self.tabBar.bounds]; _myTabbar.backgroundColor = [UIColor blueColor]; //将自定义标签栏添加在系统标签栏上
[self.tabBar addSubview:_myTabbar];
_myTabbar.userInteractionEnabled = YES;
}
-(void)createViewControllers
{
NSArray *vcArray = @[@"ViewController",
@"ViewController1",
@"ViewController2",
@"ViewController3",
@"ViewController4"]; NSMutableArray *vcs = [[NSMutableArray alloc]init];
for (int i = ; i<vcArray.count; i++) {
//反射(将字符串对象转换成对应的类对象)
UIViewController *vc = [[NSClassFromString(vcArray[i]) alloc]init];
vc.navigationItem.title = vcArray[i];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
[vcs addObject:navi];
}
self.viewControllers = vcs;
} -(void)createTabs
{
NSArray *titleArray = @[@"首页",@"社会",@"金融",@"法制",@"教育"];
NSArray *imageArray = @[@"tabbar_account",
@"tabbar_appfree",
@"tabbar_limitfree",
@"tabbar_reduceprice",
@"tabbar_subject"];
NSArray *imageSelectedArray = @[@"tabbar_account_press",
@"tabbar_appfree_press",
@"tabbar_limitfree_press",
@"tabbar_reduceprice_press",
@"tabbar_subject_press"]; for (int i = ; i<titleArray.count; i++) {
MyTabbBarItem *btn = [MyTabbBarItem buttonWithType:UIButtonTypeCustom];
[btn setTitle:titleArray[i] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:imageSelectedArray[i]] forState:UIControlStateSelected];
if (i == ) {
btn.selected = YES;
}
CGFloat width = [[UIScreen mainScreen]bounds].size.width/;
btn.frame = CGRectMake(width * i, , width, );
[_myTabbar addSubview:btn];
btn.tag = + i;
[btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
}
} -(void)selectAction:(UIButton *)btn
{
NSInteger index = btn.tag - ;
self.selectedIndex = index;
for (UIButton *btn in _myTabbar.subviews) {
btn.selected = NO;
}
//设置selected属性
btn.selected = YES;
} @end
5)AppDelegate.m 文件中的代码实现
//系统自带的标签的高度是49
//可以自定义标签来设置不同高度的标签栏
//此处不能指定导航栏否则标签栏无法显示,因为后面会指定导航栏
MyTabBarController *tabbar = [[MyTabBarController alloc]init];
self.window.rootViewController = tabbar;
self.window.backgroundColor = [UIColor whiteColor];
return YES;
3、UITabbarController的视图层级关系;利用Window 实现QQ右侧菜单视图功能
1)AppDelegate.m 代码实现
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
{
UIWindow *_w;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_w = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
_w.backgroundColor = [UIColor purpleColor]; _w.rootViewController = [RootViewController new];
[_w makeKeyAndVisible];
UITabBarController *tabbar = [[UITabBarController alloc]init];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
navi.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:];
tabbar.viewControllers = @[navi];
self.window.rootViewController = tabbar;
self.window.backgroundColor = [UIColor clearColor];
return YES;
}
2)ViewController.m window切换代码实现
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
self.title = @"首页";
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
label.backgroundColor = [UIColor redColor];
[self.view addSubview:label]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self action:@selector(testAciton)]; } -(void)testAciton
{
UIView *window = self.navigationController.tabBarController.view.superview;
[UIView animateWithDuration:1.0 animations:^{
window.center = CGPointMake(, );
window.transform = CGAffineTransformScale(window.transform, 0.8, 0.8);
}]; //视图层级关系
UIView *layoutContainerView = window.subviews[];
UIView *transitionView = layoutContainerView.subviews[];
UIView *wrapperView = transitionView.subviews[];
UIView *layoutView = wrapperView.subviews[];
UIView *naviTransitionView = layoutView.subviews[];
UIView *naviWrapperView = naviTransitionView.subviews[];
UIView *uiview = naviWrapperView.subviews[];
NSLog(@"%@",uiview.subviews);
//判断两个视图坐标是否重合或发生碰撞
//CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>);
}
3)RootViewController.m 右侧展现视图 代码实现
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(, , , );
btn.backgroundColor = [UIColor whiteColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clickAction
{
NSLog(@"==============");
}
iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)的更多相关文章
- iOS阶段学习第26天笔记(UILabel的介绍)
iOS学习(UI)知识点整理 一.关于UILabel的使用介绍 1)概念:UILabel是一个继承自UIView的用于展示文本信息的控件 2)UI中所有的控件都继承自UIView 即UIView 是U ...
- iOS阶段学习第32天笔记(页面传值方法介绍)
iOS学习(UI)知识点整理 一.界面传值方法 1.方法一 Block传值 通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码: 1)SubViewContro ...
- iOS 阶段学习第24天笔记(Block的介绍)
iOS学习(OC语言)知识点整理 一.Block 的介绍 1)概念: block 是一种数据类型,类似于C语言中没有名字的函数,可以接收参数,也可以返回值与C函数一样被调用 封装一段代码 可以在任何地 ...
- iOS 阶段学习第23天笔记(XML数据格式介绍)
iOS学习(OC语言)知识点整理 一.XML数据格式介绍 1)概念:xml是extensible markup language扩展的标记语言,一般用来表示.传输和存储数据 2)xml与json目前使 ...
- iOS 阶段学习第22天笔记(JSON数据格式介绍)
iOS学习(OC语言)知识点整理 一.JSON数据格式 1)概念:json是一种网络数据传输格式,有值/对象:{“A”:1,”B”:”2”…}词典:对象的序列:[,,,,,]数组两种数据类型 2)UR ...
- iOS阶段学习第31天笔记(UINavigationBar介绍)
iOS学习(UI)知识点整理 一.UINavigationBar 的介绍 1)概念:UINavigationBar 是用于定义导航栏按钮的一个类对象 2)在使用UINavigationBar之前必须先 ...
- iOS阶段学习第30天笔记( UIViewController—Delegate(代理) )
iOS学习(UI)知识点整理 一.UIViewController的介绍 1)概念:UIViewController 即视图控制器,用来管理和控制页面跳转的一个类 ,iOS里面采用了MVC的体系结构, ...
- iOS阶段学习第21天笔记(ARC内存管理-Copy-代理)
iOS学习(OC语言)知识点整理 一.OC 中的ARC内存管理 1)ARC中释放对象的内存原则:看这个对象有没有强引用指向它 2)strong:强引用,默认情况下的引用都是强引用 3) weak:弱引 ...
- iOS阶段学习第18天笔记(Plist-Archiver-归档与解归档操作)
iOS学习(OC语言)知识点整理 一.归档与解归档的操作 1)归档是一个过程,将一个或多个对象存储起来,以便以后可以还原,包括将对象存入文件,以后再读取 将数据对象归档成plist文件 2)plist ...
随机推荐
- 微软CMS项目 Orchard 所用到的开源项目
研发了Orchard一年左右了,时常遇到瓶颈,总觉得力不从心,其实并不是基础不够,关键还是概念性的东西太多,一会儿这个概念名词,一会那个,关于Orchard的技术文档也的确很少,每次看起来总是焦头烂额 ...
- ORA-12899: value too large for column (actual: 27, maximum: 20)
导入数据时报错以下错误,这是因为原来的数据库是GBK的,每个汉字两个字节,但新数据库是UTF-8的,每个汉字是三个字节,导致超过长度了. ORA-12899: value too large for ...
- Html5 实现灯笼绘制
最近在学习Html5,就用JavaScript在Canvas试着绘制了一个灯笼,并作了简要的说明. 具体绘制思路在页面上有说明,不再赘述,代码如下: <script type="tex ...
- jsp的 javascript中 嵌套 html 注释
看到公司的代码,我也是蛋疼了,各种乱. 还发现有很多的jsp的 javascript中 嵌套 html 注释, 这个可行? 我之前可是没用过. 后面查找各种资料发现,这个也是可行的,主要是为了兼容不支 ...
- Spark读取HBase
背景:公司有些业务需求是存储在HBase上的,总是有业务人员找我要各种数据,所以想直接用Spark( shell) 加载到RDD进行计算 摘要: 1.相关环境 2.代码例子 内容 1.相关环境 Spa ...
- js array queue (队列)
前言 今天项目中做一个图片效果展示,需要实时从后台获取图片数据,前段做展示.想想用到队列,比较好实现这个功能,只需要展示队列里的数据就可以了.于是写了个js 对列. js code /** * [Qu ...
- WCF 安全性 之 Windows
案例下载 http://download.csdn.net/detail/woxpp/4113172 服务端配置代码 <system.serviceModel> <services& ...
- react Props 验证 propTypes,
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- SQL Server 解读【已分区索引的特殊指导原则】(1)- 索引对齐
一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的 ...
- jQuery源码分析系列(36) : Ajax - 类型转化器
什么是类型转化器? jQuery支持不同格式的数据返回形式,比如dataType为 xml, json,jsonp,script, or html 但是浏览器的XMLHttpRequest对象对数据的 ...