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. love is ... ...

    16 years old, love is dream.20 years old, love is sex.30 years old, love is marriage. 40 years old, ...

  2. JMeter学习(三十三)测试报告优化(转载)

    转载自 http://www.cnblogs.com/yangxia-test 如果按JMeter默认设置,生成报告如下: 从上图可以看出,结果信息比较简单,对于运行成功的case,还可以将就用着.但 ...

  3. easyui中自定义下拉框的使用

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Redis数据结构的多种用法

    分布式锁(string) setnx key value,当key不存在时,将 key 的值设为 value ,返回1.若给定的 key 已经存在,则setnx不做任何动作,返回0. 当setnx返回 ...

  5. TOJ 4829: 计算器的改良

    Python写法! 传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4829 描述 N ...

  6. 装了appserv之后,浏览器中访问localhost加载不了

    AppServe下载地址:https://AppServnetwork.com/ 如果只下载Apache,推荐大神博客http://www.cnblogs.com/zhaoqingqing/p/496 ...

  7. MDK生成.bin

    方法1: 默认选择编译输出的路径输出bin fromelf.exe --bin -o "$L@L.bin" "#L" 保存编译 方法2: 在要输出的目录下,新建 ...

  8. 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口

    1.可靠传输 (1)三次握手 TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接: (1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_S ...

  9. selenium验证码和错误截图

    验证码的识别: 1,破解验证码 OCR识别(一般使用tesseract-ocr) 人工智能(AI机器学习 TensorFlow,成本大) 2,绕过验证码 1, 让开发人员临时关闭验证码 2,提供万能验 ...

  10. [leetcode]101. Symmetric Tree对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...