您的位置:首页 > 产品设计 > UI/UE

UINavigationController导航控制器 UINavigationBar导航栏

2016-01-14 17:42 441 查看
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入栈就是栈顶元素,显示的就是second的view

[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 action:nil];

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: