UINavigation的一些常用用法(持续更新-2016年10月27日)
2016-10-27 14:13
387 查看
先看示例程序:
完整代码如下:
AppDelegate.m
#import "AppDelegate.h" #import "ViewController.h" #import "SecondViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; ViewController * vc = [[ViewController alloc]init]; UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc]; UITabBarController * tabBar = [[UITabBarController alloc]init]; tabBar.viewControllers = @[nav]; self.window.rootViewController = tabBar; //更改导航栏背景颜色(所有的导航栏都颜色变了,当然,这句话写在viewController的viewdidload中也可以) vc.navigationController.navigationBar.barTintColor = [UIColor yellowColor]; return YES; }
ViewController.m
#import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @end @implementation ViewController -(id)init { if (self = [super init]) { self.title = @"首页"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200,40)]; btn.center = self.view.center; btn.layer.cornerRadius = 10; btn.layer.masksToBounds = YES; btn.backgroundColor = [UIColor orangeColor]; [btn setTitle:@"跳转到第二个页面" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(clickJump) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; // 更改navigation字体颜色,(所有的都变成红色了,第一个是过时的写法,第二个是新写法) // [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor] ,UITextAttributeTextColor, nil]]; [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}]; // 系统自带的rightbarbuttonitem(带图片)[1] UIBarButtonItem* right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"finish"] style:UIBarButtonItemStylePlain target:self action:@selector(right)]; //rightBarButtonItem也可以换成leftBarButtonItem self.navigationItem.rightBarButtonItem = right; // 更改返回按钮颜色 self.navigationController.navigationBar.tintColor = [UIColor purpleColor]; // 更改返回按钮标题 UIBarButtonItem * backItem = [[UIBarButtonItem alloc]init]; backItem.title = @"返回"; self.navigationItem.backBarButtonItem = backItem; // 更改导航栏背景颜色(写在APPdelegate.m中也可以,但是下面两种写法的效果区别)[2] // self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor]; // self.navigationController.navigationBar.barTintColor = [UIColor yellowColor]; } -(void)right { NSLog(@"点击了右上角按钮"); } -(void)clickJump { [self.navigationController pushViewController:[[SecondViewController alloc]init] animated:YES]; } @end
[1]注意:此处可以换成系统自带的,如:
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(right)];
效果图:
也可以直接设置成文字的形式,如:
UIBarButtonItem * finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
效果图:
[2]注意:AppDelegate.m中没有XX.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];这种写法,若把更改导航栏的颜色代码写在viewdidload中,则二者的直观区别和图层区别分别为:
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
显而易见,二者的颜色为何不一样,就是因为两种方法中,颜色层的位置不一致。
SecondViewController.m
#import "SecondViewController.h" #import "ThirdViewController.h" @interface SecondViewController () @end @implementation SecondViewController //页面跳转过后,底部的bottombar消失,返回上级页面,它会再次出现[3] -(BOOL)hidesBottomBarWhenPushed { return YES; } -(id)init { if (self = [super init]) { self.title = @"第二个页面"; // self.navigationItem.title = @"第二个页面"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; // [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor] ,UITextAttributeTextColor, nil]]; // 更改右上角按钮 UIBarButtonItem * finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)]; self.navigationItem.rightBarButtonItem = finish; UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200,40)]; btn.center = self.view.center; btn.layer.cornerRadius = 10; btn.layer.masksToBounds = YES; btn.backgroundColor = [UIColor orangeColor]; [btn setTitle:@"返回到第一个页面" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(clickJump) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } -(void)clickJump { NSLog(@"返回到第一页面"); [self.navigationController popViewControllerAnimated:YES]; } -(void)finish{ NSLog(@"点击了完成按钮"); } @end
[3]注意:若把
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}
改成:
self.navigationController.hidesBottomBarWhenPushed = YES;
则,底部bottombar无论如何也不会消失
若改成:
self.tabBarController.tabBar.hidden = YES;
则,底部bottombar将在页面跳转后永远消失,即使返回上级页面也不会出现
多级页面跳转到指定页面的方法
for (UIViewController *controller in self.navigationController.viewControllers) { if ([controller isKindOfClass:[SecondViewController class]]) { [self.navigationController popToViewController:controller animated:YES]; } }
相关文章推荐
- ABAP report常用的一些语句用法(包括字符串操作,变量动态操作)(持续更新)
- UILable和UITextField的一些用法(持续更新)
- linux下一些常用操作的命令及快捷键(持续更新中。。。。。。。。。。)
- 一些常用的linux命令(持续更新)
- 关于CSS常用用法(持续更新)
- iOS学习之——学习中遇到的一些常用的方法及一些注意事项(持续更新)
- Hadoop的一些常用工具和技巧 【持续更新】
- Monorail的一些常用的东西(验证码,分页。。。持续更新)
- 一些css小用法总结(持续更新~)
- 我所接触到的一些常用Linux命令和用法(初学者献丑了)每日更新.......
- UILabel的一些不常用属性,持续更新
- STL中常用的一些算法函数[持续更新]
- ImageButton、Button、TextView、EditText等一些常用控件的属性(持续更新)
- delphi的一些常用操作技巧,持续更新中。。。
- 项目开发过程中一些常用的工具[持续更新中]
- STL中常用的一些算法函数[持续更新
- linux mysql 运维一些常用记录(持续更新中...)
- sqlserver常用的一些操作(持续更新)
- Winform常用的一些功能收集(持续更新)
- iOS开发中对于一些常用的相对路径(持续更新)