NavigationController

本文详细介绍UINavigationController的使用方法,包括创建、管理视图控制器切换、配置导航栏样式及添加按钮等,帮助读者掌握iOS应用中导航流程的设计与实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.描述
继承自UIViewController,依然是一种控制器,但是没有具体的view,是管理控制器的控制器
优点:能够管理和控制VC的走向,比present方式更清晰

2.创建
//创建导航控制器,将firstVc设置为导航控制器的根视图控制器
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:firstVc];

3.方法
//firstVC 通知自己的导航控制器,推出视图 secondVC
[self.navigationController pushViewController:secondVC animated:YES];
//通知自己的试图控制器,返回上一张视图
[self.navigationController popViewControllerAnimated:YES];

4.代理方法

  • (UIBarPosition)positionForBar:(id)bar{
    return UIBarPositionTopAttached;
    }

5.导航栏
5.1 配置
//获取导航栏
UINavigationBar *navi=self.navigationController.navigationBar;
//配置导航栏的背景色
navi.barTintColor=[UIColor redColor];
//配置导航栏是否透明为No
navi.translucent=NO;
//设置导航栏在默认状态下的背景图片
[navi setBackgroundImage:[UIImage imageNamed:@”NavigationBarDefault”] forBarMetrics:UIBarMetricsDefault];
//设置导航栏在横屏状态下的背景图片
[navi setBackgroundImage:[UIImage imageNamed:@”NavigationBarLandscapePhone”] forBarMetrics:UIBarMetricsLandscapePhone];
//设置返回按钮图片第二步
navi.backIndicatorTransitionMaskImage=[UIImage imageNamed:@”NavigationBarLandscapePhone”];
//设置返回按钮图片第一步
navi.backIndicatorImage=[UIImage imageNamed:@”NavigationBarLandscapePhone”];
//设置导航栏左右两边按钮的字的颜色
navi.tintColor=[UIColor redColor];
//设置title的属性
navi.titleTextAttributes=@{NSFontAttributeName: [UIFont systemFontOfSize:24],NSForegroundColorAttributeName:[UIColor redColor]};
5.2 属性
// 设置导航栏 title 显示 @” 小兔子系统 ”
self.title=@”小兔子系统”;
//创建一个导航栏按钮,当点击时,对象self实现方法goScondVC:
UIBarButtonItem *rightbutton=[[UIBarButtonItem alloc]initWithTitle:@“下一个” style:0 target:self action:@selector(goScondVC:)];
self.navigationItem.rightBarButtonItem=rightbutton;//将创建的导航栏按钮给导航栏
self.navigationItem.rightBarButtonItems=@[rightbutton1,rightbutton2];//将创建好的两个按钮给导航栏
5.3 方法
[self.navigationController setNavigationBarHidden:YES animated:YES];//设置导航栏隐藏为yes
6.工具栏
// 设置工具栏隐藏为 No ,范围为导航控制器管理的所有VC都生效
self.navigationController.toolbarHidden=NO;
//弹簧特效
UIBarButtonSystemItemFlexibleSpace
//木棍特效
UIBarButtonSystemItemFixedSpace
//设置按钮宽度
rightbutton2.width=40;
//设置在推出视图secondVC时,secondVC的底部的菜单栏是否隐藏
secondVC.hidesBottomBarWhenPushed=YES;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值