UINavigationController导航控制器 UINavigationBar导航栏

UINavigationController:导航控制器

继承于UIViewController,采用栈的方式管理视图控制器对象,当切换到下个界面时执行Push入栈操作,返回执行pop出栈操作

一般用于复杂的分层数据

RootViewController *root = [[RootViewController alloc]init];

UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:root];

self.window.rootViewController = nav;


SecondViewController *second = [[SecondViewController alloc]init];

self.navigationController获取管理self的导航控制器对象,前提是self已经放入导航控制器的栈中

second入栈就是栈顶元素,显示的就是secondview

[self.navigationController pushViewController:second animated:YES];


出栈

[self.navigationController popViewControllerAnimated:YES];


直接切换到根视图控制器,自动将栈底上面的元素全部移除

[self.navigationController popToRootViewControllerAnimated:YES];


获取导航控制器管理的视图控制器,返回数组,视图控制器在数组中的顺序与显示的顺序一致

NSArray *array = self.navigationController.viewControllers;

切换到某个视图控制器

[self.navigationController popToViewController:array[1] animated:YES];


UINavigationBar:导航栏

不能设置背景颜色,但是可以和barStyle结合使用产生一些效果

self.navigationController.navigationBar.backgroundColor = [UIColor redColor];


设置导航栏样式,四个枚举值,实际上2种效果

self.navigationController.navigationBar.barStyle = UIBarStyleDefault;


设置导航栏文本颜色

self.navigationController.navigationBar.tintColor = [UIColor greenColor];


设置导航栏背景色

self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];


设置导航栏背景图片

UIBarMetricsCompact:设备处于肖像模式时没有背景图片,只有处于风景模式时显示背景图片

UIBarMetricsCompact:任何模式下都有背景图片

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@“xx”] forBarMetrics:UIBarMetricsDefault];

设置是否裁剪图片

self.navigationController.navigationBar.clipsToBounds = YES;


设置是否隐藏导航栏

self.navigationController.navigationBar.hidden = YES;


设置当前视图控制器的导航栏上的标题,从下个界面返回时左侧的按钮项自动显示为当前的标题,如果标题过长,恢复成back

self.navigationItem.title = @“Root”;


设置视图控制器的标题

//self.title=@“Home”;

UILabel *label ….


设置标题视图,view会自动在导航栏中间显示

self.navigationItem.titleView = label;


设置导航栏左侧按钮

创建一个按钮对象:初始化时指定标题样式,单击时响应的对象和方法

UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@“Left” style:UIBarButtonItemStyleDone target:self action:nil];

创建一个按钮对象,初始化时指定图片、样式,单击时响应的对象和方法

UIImage *image = [UIImage imageNamed:@“xxx.png”];

设置图片的渲染模式:总是采用原始图片,不渲染

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self actionnil];

NSArray *array = @[item1,item2];

self.navigationItem.leftBarButtonItems = array;


设置导航条标题颜色和字体大小

[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:19],NSForegroundColorAttributeName:[UIColor colorWithRed:0.18f green:0.15f blue:0.11f alpha:1.00f]}];


//隐藏系统自带的返回按钮

    self.navigationItem.hidesBackButton = YES;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值