#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;
}
#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];
#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];
}
}
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:UINavigation的一些常用用法(截止-2016年10月27日)