1.先說添加吧
AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 修改導航顏色 [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:100 green:80 blue:50 alpha:1]]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc] init]; self.window.rootViewController = vc; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
2.自定義導航欄
系統自帶的方法 //左邊按鈕 UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(selectLeftAction:)]; self.navigationItem.leftBarButtonItem = leftButton; //右邊按鈕 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectRightAction2:)]; self.navigationItem.rightBarButtonItem = rightButton;
自定義按鈕圖案
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self scaleToSize:[UIImage imageNamed:@"myselect.png"] size:CGSizeMake(20, 20)] style:UIBarButtonItemStylePlain target:self action:@selector(Actionright:)]; //設置圖片大小 - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ // 創建一個bitmap的context // 並把它設置成為當前正在使用的context UIGraphicsBeginImageContext(size); // 繪制改變大小的圖片 [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; // 從當前context中創建一個改變大小后的圖片 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // 使當前的context出堆棧 UIGraphicsEndImageContext(); // 返回新的改變大小后的圖片 return scaledImage; }
設置push返回按鈕的樣式
//push返回按鈕樣式 UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = item; //self.navigationController.navigationBar.tintColor = [UIColor grayColor];
自定義標題與導航欄的樣式
//修改導航欄標題字體大小和顏色,背景顏色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:212/255.0 green:51/255.0 blue:36/255.0 alpha:1]]; [self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17], NSForegroundColorAttributeName:[UIColor whiteColor] }];
這是改變最上邊電量圖標,時間等顏色
3.關於跳轉的一些總結:
(1).push跳轉到下一頁,會帶着自己導航欄一起跳,這里說的導航欄說的是他自定義的導航欄的屬性
NextViewController *next =[[NextViewController alloc]init];
[self.navigationController pushViewController:next animated:NO];
(2).push跳轉到下一頁,下一頁隱藏導航欄
//隱藏導航欄 -(void)viewWillAppear:(BOOL)animated{ self.navigationController.navigationBarHidden = YES; }
(3).pop到前面的任何一頁面
//返回視圖根控制器 [self.navigationController popToRootViewControllerAnimated:YES]; //pop到指定頁面 // for (UIViewController *controller in self.navigationController.viewControllers) { // if ([controller isKindOfClass:[NextViewController class]]) { // NextViewController *A =(NextViewController *)controller; // [self.navigationController popToViewController:A animated:YES]; // } // } //其中的NextViewController為想要跳轉到的view
(4).present不帶導航欄的跳轉
HomeViewController *home = [[HomeViewController alloc]init]; [self presentViewController:home animated:YES completion:^{ NSLog(@"Login Success!"); }];
如果是拖頁面的話
HomeViewController *home = [self.storyboard instantiateViewControllerWithIdentifier:@"home"]; home.sessionID = self.sessionID; [self presentViewController:home animated:YES completion:^{ NSLog(@"Login Success!"); }];