UINavigationController 知识整理

UINavigationController

导航控制器,是通过栈来管理视图控制器的,栈遵循先进后出的原则


实例化,设置根视图控制器

     ViewController *vi = [[ViewController alloc] init];
     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: vi];

页面操作

//跳转到second
     [self.navigationController pushViewController:second animated:YES];

//返回上一页 将本页出栈
     [self.navigationController popViewControllerAnimated:YES];

//返回首页 除首页均出栈
     [self.navigationController popToRootViewControllerAnimated:YES];

//返回任意一页
     //[0,1,2,3,4]
     //[first,second,third,four,five]
     NSArray *array = self.navigationController.viewControllers;
     [self.navigationController popToViewController:array[1] animated:YES];

导航栏 NavigationBar

//更改样式
    self.navigationController.navigationBar.barStyle =UIBarStyleDefault;
    //UIBarStyleBlackOpaque        不透明
    //UIBarStyleBlackTranslucent   半透明

//更改颜色
    //backgroundColor 颜色不纯正
    self.navigationController.navigationBar.backgroundColor = [UIColor cyanColor];
    //barTintColor 颜色纯正
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    //“back”的颜色
    self.navigationController.navigationBar.tintColor = [UIColor redColor];

//背景图片
     //竖屏:UIBarMetricsDefault
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
    //横屏:
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-32.png"] forBarMetrics:UIBarMetricsLandscapePhone];

//隐藏导航栏
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];

全局更改title创建出来的文字大小及颜色 在AppDelegat.m

// NSForegroundColorAttributeName 文字颜色的key 颜色为value
// NSFontAttributeName 文字大小的key 文字大小值为value
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
    [[UINavigationBar appearance]setTitleTextAttributes:dic];
//文字类型
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(buttonDown:)];
    self.navigationItem.leftBarButtonItem = left;

//图片类型
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonUp:)];
    self.navigationItem.rightBarButtonItem = right;

//系统图片类
    UIBarButtonItem *systemImage = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(buttonSys:)];
    self.navigationItem.leftBarButtonItem = systemImage;

//自定义按钮类
    UIBarButtonItem *custom = [[UIBarButtonItem alloc]initWithCustomView:button1];
    self.navigationItem.rightBarButtonItem = custom;

//多按钮 数组元素为UIBarButtonItem实例化出来的对象
    self.navigationItem.leftBarButtonItems = @[left,right];
    /*
      左按钮 按数组顺序由左向右展示
      右按钮 由右向左展示
      用同一个数组会有误差 不会重复使用图标
    */

//隐藏系统自带按钮 如果leftBarButtonItem有值,系统自带返回值按钮覆盖
    [self.navigationItem setHidesBackButton:YES];
UIToolBar
//显示/隐藏
    self.navigationController.toolbarHidden = NO;

//设置颜色
     self.navigationController.toolbar.barTintColor = [UIColor redColor];

//设置背景图片
     [self.navigationController.toolbar setBackgroundImage:img forToolbarPosition:UIBarPositionBottom barMetrics:UIbarMetricsDefault];

//添加按钮
     self.toolbarItems = @[ , , ,];

//创建空白按钮 自动均分空白部分
     UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:SEL];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值