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. SQLMAP自动注入(四):枚举

    --privileges 查询权限 -U 指定用户 -CU指定当前用户 --schema 查询所有的数据 --batch 批处理,自动选择默认选项 --exclude-sysdbs 排除系统库的查询 ...

  2. js保留小数点后面几位的方法

    原文地址: http://www.jb51.net/article/45884.htm 四舍五入以下处理结果会四舍五入: ? 1 2 var num =2.446242342; num = num.t ...

  3. dedecms 5.7sp2在用type标签时出现调用无效问题

    {dede:type typeid='1'}栏目{/dede:type}   无效 和 {dede:type typeid='1'}[field:typename/]{/dede:type}   有效 ...

  4. hbase备份数据与异地新建

    hbase org.apache.hadoop.hbase.mapreduce.Driver export news /tmp/news1 备份news表至hdfs的/tmp目录下面. hadoop ...

  5. pta l2-18(多项式A除以B)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984 题意:给定两个多项式,求出其做除法 ...

  6. 数论----gcd和lcm

    gcd即最大公约数,lcm即最小公倍数. 首先给出a×b=gcd×lcm 证明:令gcd(a,b)=k,a=xk,b=yk,则a×b=x*y*k*k,而lcm=x*y*k,所以a*b=gcd*lcm. ...

  7. 199. Binary Tree Right Side View (Tree, Stack)

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  8. 数据库(mysql)

    一.left join  right join  inner join left join(左连接),在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录. right join(右 ...

  9. Bootstrap(7) 输入框和导航组件

            一.输入框组件 文本输入框就是可以在<input>元素前后加上文字或按钮,可以实现对表单控件的扩展. //在左侧添加文字 <!-- //在左侧添加文字 --> ...

  10. docker-ce-17.09 数据卷和数据卷容器

    docker容器中管理数据两种方式:1.数据卷(Data Volumes)2.数据卷容器(Data Volume Dontainers) 一.数据卷特性:1.数据卷可以在容器之间共享和重用2.对数据卷 ...