1、AppDelegate.m老生常谈了,创建window,创建根视图rootViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
//权限最高的给根视图控制器
self.window.rootViewController = rootVC; return YES;
}

2、在根视图中弄一个三级导航样式出来

RootViewController.h

@interface RootViewController : UITabBarController//我们的根视图RootViewController继承自UITabBarController
@property (nonatomic, strong) UIView *tabBarView;//声明自定义的tabBar,包括下面的方法show写在这里是为了方便其他viewContoller控制
- (void)showTabBar:(BOOL)show;//控制是否显示tabBar,tabBar其实就是屏幕下方的那个标签导航
@end

RootViewController.m

#define kScreenWidth [UIScreen mainScreen].bounds.size.width     //宏定义屏幕宽度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height //宏定义屏幕高度
CGFloat const tabViewHeight = 49;
CGFloat const btnWidth = 64;
CGFloat const btnHeight = 45;
@interface RootViewController ()
@property (nonatomic, strong) UIImageView *selectView; //在tabBar上的选中效果
@end - (void)viewDidLoad {
[super viewDidLoad];
self.tabBar.hidden = YES;//隐藏系统默认的样式
[self initViewController];
[self initTabBarView]; }
//初始化视图控制器,这里就是把根控制器要控制的视图统统加进来
- (void)initViewController{
//初始化视图控制器
ProfielViewController *profielVC = [[ProfielViewController alloc] init];
MessageViewController *messageVC = [[MessageViewController alloc] init];
ColaViewController *colaVC = [[ColaViewController alloc] init];
UserViewController *userVC = [[UserViewController alloc] init];
MoreViewController *moreVC = [[MoreViewController alloc] init];
NSArray *vcArray = @[profielVC,messageVC,colaVC,userVC,moreVC];
NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];//初始化可变数组,虽然指定了长度,但他仍然是可变的
//初始化导航控制器
for (int i = 0; i < vcArray.count; i++) {
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:vcArray[i]];//创建若干个导航控制器,导航控制器里是刚创建的的profielVC等视图
[tabArray addObject:navCtrl];//加入到数组中
}
//将导航控制器给标签控制器
self.viewControllers = tabArray;//将根控制器的所控制的视图加进来
}
//自定义底部的标签工具栏
- (void)initTabBarView{
//初始化标签工具栏视图
_tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - tabViewHeight, kScreenWidth, tabViewHeight)];//下划线的功能是让编译器自动生成getter方法,并指定frame
_tabBarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mask_navbar"]];
[self.view addSubview:_tabBarView];//显示tabBarView
//新语法创建数组,拿到图片
NSArray *imgArray = @[@"home_tab_icon_1",@"home_tab_icon_2",@"home_tab_icon_3",@"home_tab_icon_4",@"home_tab_icon_5"];
for (int i = 0; i < imgArray.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:imgArray[i]] forState:UIControlStateNormal];
btn.frame = CGRectMake(btnWidth * i, (tabViewHeight - btnHeight)/2, btnWidth, btnHeight);
btn.tag = 100 + i;//下面的TouchUpInside事件需要btn指定要切换到哪个视图,这里的tag作为传值的作用。100以下的tag值IOS占用了,所以要设置100以上的值
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];//点击底部标签,改变标签上按钮的样式,这里有些不明白为什么它会自动能找到对应的视图。
[self.tabBarView addSubview:btn];
}
//初始化选中图片视图
_selectView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btnWidth, btnHeight)];
_selectView.image = [UIImage imageNamed:@"home_bottom_tab_arrow"];
[_tabBarView addSubview:_selectView];
}
#pragma mark - UIButtonAction
- (void)btnAction:(UIButton *)button{
//根据tag值判断当前索引
self.selectedIndex = button.tag - 100;
[UIView animateWithDuration:0.2 animations:^{
_selectView.center = button.center;//选中视图中,tabBar的箭头滑动效果
} completion:nil];
}
//是否显示工具栏
- (void)showTabBar:(BOOL)show{
CGRect frame = self.tabBarView.frame;
if (show) {
frame.origin.x = 0;
}else{
frame.origin.x = - kScreenWidth;
}
//重新赋值frame
[UIView animateWithDuration:0.2 animations:^{
self.tabBarView.frame = frame;
} completion:nil];
}

3、我们个性化一下顶部的导航栏

CGFloat const writeButtonWidth = 33;
CGFloat const writeButtonHeight = 32;
@interface ProfielViewController () @end @implementation ProfielViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"首页";
self.view.backgroundColor = [UIColor yellowColor];
[self initNavButton];
[self initPushButton];
}
//自定义顶部导航栏按钮
- (void)initNavButton{
UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
writeBtn.frame = CGRectMake(0, 0, writeButtonWidth, writeButtonHeight);
[writeBtn setBackgroundImage:[UIImage imageNamed:@"write"] forState:UIControlStateNormal];
[writeBtn addTarget:self action:@selector(presentAction) forControlEvents:UIControlEventTouchUpInside];//弹出一个model类型的view
//添加自定义按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:writeBtn];//UIBarButtonItem不能随意摆放在屏幕上,它不是继承自UIView。它可以放在导航栏,标签栏或工具栏管理
self.navigationItem.rightBarButtonItem = item;//放到导航栏上
}
//初始化push按钮
- (void)initPushButton{
UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pushButton.frame = CGRectMake(100, 100, 200, 40);
//[pushButton setImage:<#(UIImage *)#> forState:<#(UIControlState)#>]
//标题和图片不能同时设置
[pushButton setTitle:@"Push" forState:UIControlStateNormal];
[pushButton addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushButton];
}
- (void)pushAction{
PushViewController *pushVC = [[PushViewController alloc] init];
[self.navigationController pushViewController:pushVC animated:YES];
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:NO];//不显示底部标签导航栏
//[self.navigationController showViewController:<#(UIViewController *)#> sender:<#(id)#>]
}
- (void)presentAction{
ModalViewController *modalVC = [[ModalViewController alloc] init];
//模态视图
[self presentViewController:modalVC animated:YES completion:nil];
}
//视图将要出现的时候调用
//这里用于push的view返回,要重新显示底部标签导航栏
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:YES];
}

IOS初级:导航控制器的更多相关文章

  1. iOS:导航控制器侧滑出栈实现

    介绍:在iOS中,导航控制器UINavigationController是默认实现左侧边缘侧滑手势出栈的,但是如果当开发者对导航控制器子控制实现自定义leftBaButtonItem时,这个侧滑功能就 ...

  2. IOS UINavigationController 导航控制器

    /** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...

  3. IOS中导航控制器的代理及隐藏控制器刚出现时的滚动条

    一.导航控制器的代理 1.UINavigationController的delegate属性 2.代理方法 1> 即将显示新控制器时调用 /* navigationController : 导航 ...

  4. IOS之导航控制器

    UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图.任何类型的视图控制器都可放入栈中.在设计导航控制器时需要指定根视图即用户看到的第一个视图.根视图 ...

  5. iOS结合导航控制器和标签栏控制器

    <span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name=& ...

  6. iOS 隔离导航控制器

    题外话:最近这两个月一直很闲,项目上基本没有啥大的需求.对于程序员来说,如果没有需求其实是一件很难受的事情,之前好多次在项目中没事找事,该优化的优化,该整理的整理.可能好多程序员都遇到过与我类似的情况 ...

  7. IOS之导航控制器传值

    UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显 ...

  8. iOS开发UINavigation——导航控制器UINavigationController

    iOS开发UINavigation系列一——导航栏UINavigtionBar摘要iOS中的导航条可以附着于导航控制器之中使用,也可以在controller中单独使用,这篇博客,主要讨论有关导航栏的使 ...

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

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

随机推荐

  1. python 之面向对象

    继承粗体文本 标签(空格分隔): 继承 什么是继承: 继承是指类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代买重用问题,继承是一种创建新类的方式,在py ...

  2. mac通过命令行获取证书和配置文件过期时间

      背景:ios打包证书的profile配置文件过期了,导致以前已经打完的测试包不能安装.所以需要加上检测机制,在打包时提示证书是否将要过期,如果要过期了给出提示   方案: 1.查找profile配 ...

  3. java面试题:多线程与并发

    多线程 关键词:线程池 Q:如何新建一个线程? 继承Thread,或者实现Runnable接口,或者通过Callable接口实现. Q:Callable怎么用? Callable可以作为FutureT ...

  4. beanstalkd 说明文档

    BEANSTALKD(1) BEANSTALKD(1) NAME beanstalkd - simple, fast work queue SYNOPSIS beanstalkd [options] ...

  5. 【工具引入】uiautomatorviewer 查找元素后自动生成代码

    缘起 公司部门调整PC部门和无线部门合并,原本负责主站PC端自动化的同事需要马上上手安卓,IOS自动化.对于初次接触移动端的测试者来说,跨度还是有点大的.加之人员有些变动,不得不搞个工具降低学习成本, ...

  6. tcp/ip通信第5期之服务器端程序

    /* 此程序是tcp/ip通信服务器端程序,测试运行在redhat5上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> # ...

  7. expect命令自动登录ssh

    expect是简单的工具原因,依赖于tcl. 直接apt安装就行. 四个关键字: spawn,派生出新进程. expect,期待得到的字符串,可以模式匹配. send,向进程发送字符串. intera ...

  8. TZOJ 3315 买火车票(线段树区间最小值)

    描述 Byteotian州铁道部决定赶上时代,为此他们引进了城市联网.假设城市联网顺次连接着n 个市从1 到n 编号(起始城市编号为1,终止城市编号为n).每辆火车有m个座位且在任何两个运送更多的乘客 ...

  9. 阿里读写分离数据源SELECT LAST_INSERT_ID()获取不到id

    异常现象 insert 通过 mybatis 以下语法给领域类 赋予的 id 值为0. 后续根据主键的update操作失效.且无异常抛出 <selectKey keyProperty=" ...

  10. exercise 1-6

    [买菜] package Practice06; import java.io.File; import java.io.FileNotFoundException; import java.util ...