NavigationController:导航控制器,一个月前接触的时候,这个地方比较让我懵~逼,主要原因是没有搞清楚IOS各种View控件的关系,每次查各位前辈都给出了官方的图,这个图不经意间都看了好多遍了,所以慢慢就理解NavigationController在视图中所处的位置了,照例上图(看水果的介绍):
So~~,如果window是最底层的话,那么接下来就是Navigation,因为我们看到和使用的比较基础的应用,都使用到了位于页面最上方和最下方的导航栏,而这些导航栏是NC提供的。公司的师傅叫我先尽量不要用storyboard来建立页面,因为之前项目都是手码的页面,团队更好维护,所以在新建完一个项目后,如何快速搭建起一个可用的视图框架呢?下面是我搜集的方法,比较程式化列出过程,希望有所帮助:
一、搭建基础框架——window、View Controller、NavigationViewController
#import <UIKit/UIKit.h>
@interfaceAppDelegate:UIResponder<UIApplicationDelegate>
@property(strong,nonatomic)UINavigationController*navigationController;
@property(strong,nonatomic)UIWindow*window;
@end
在这里我们大家各种视图控制器ViewController,由于需要在每个页面转为Active时,就应该把视图加载好,所以这部分工作放在didFinishLaunchingWithOptions中。
再提一点,就是我们页面框架无非就是那么几类,比如在最下方,有几个导航按钮,每个按钮对应一个页面,这种页面框架有个特殊的建立方式,就是UITabBarController,这个类中封装了这类页面需要的元素,比如那几个导航按钮;
又或者还有一种页面,直接就是一个上方有个导航栏的单页面,无非在加个左右按键啥的或者segmented button,这种就只是单纯的NavigationController了。
下面基于这两种结构建立页面
#import "AppDelegate.h"
#import "Item1ViewController.h"
#import "Item2ViewController.h"
#import "Item3ViewController.h"
#import "Item4ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//shemging and set root VC
UITabBarController *tabBarController = [[UITabBarController alloc]init];
self.window.rootViewController = tabBarController;
//init 4 VC on this TabBatController
Item1ViewController *vc1 = [[Item1ViewController alloc]init];
Item2ViewController *vc2 = [[Item2ViewController alloc]init];
Item3ViewController *vc3 = [[Item3ViewController alloc]init];
Item4ViewController *vc4 = [[Item4ViewController alloc]init];
UINavigationController *navc1 = [[UINavigationController alloc]initWithRootViewController:vc1];
UINavigationController *navc2 = [[UINavigationController alloc]initWithRootViewController:vc2];
UINavigationController *navc3 = [[UINavigationController alloc]initWithRootViewController:vc3];
UINavigationController *navc4 = [[UINavigationController alloc]initWithRootViewController:vc4];
NSArray *controllers = [NSArray arrayWithObjects:navc1,navc2,navc3,navc4, nil];
tabBarController.viewControllers = controllers;
//item title
navc1.title = @"1";
navc2.title = @"2";
navc3.title = @"3";
navc4.title = @"4";
//insert image
navc1.tabBarItem.image = [UIImage imageNamed:@"Office_Excel.png"];
navc2.tabBarItem.image = [UIImage imageNamed:@"Office_Frontpage.png"];
navc3.tabBarItem.image = [UIImage imageNamed:@"Office_Words.png"];
navc4.tabBarItem.image = [UIImage imageNamed:@"OE3.png"];
//show
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import"AppDelegate.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //1.创建Window self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; //a.初始化一个tabBar控制器 UITabBarController *tb=[[UITabBarController alloc]init]; //设置控制器为Window的根控制器 self.window.rootViewController=tb; //b.创建子控制器 UIViewController *c1=[[UIViewController alloc]init]; c1.view.backgroundColor=[UIColor grayColor]; c1.view.backgroundColor=[UIColor greenColor]; c1.tabBarItem.title=@"消息"; c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"]; c1.tabBarItem.badgeValue=@"123"; UIViewController *c2=[[UIViewController alloc]init]; c2.view.backgroundColor=[UIColor brownColor]; c2.tabBarItem.title=@"联系人"; c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"]; UIViewController *c3=[[UIViewController alloc]init]; c3.tabBarItem.title=@"动态"; c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"]; UIViewController *c4=[[UIViewController alloc]init]; c4.tabBarItem.title=@"设置"; c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"]; //c.添加子控制器到ITabBarController中 //c.1第一种方式 // [tb addChildViewController:c1]; // [tb addChildViewController:c2]; //c.2第二种方式 tb.viewControllers=@[c1,c2,c3,c4]; //2.设置Window为主窗口并显示出来 [self.window makeKeyAndVisible]; return YES; } @end
2-2 基于NavigationController
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//1
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
//2
ViewController *rootView = [[ViewController alloc]init];
rootView.title = @"root view";
//3
self.navigationController = [[UINavigationController alloc]init];
[self.navigationController pushViewController:rootView animated:YES];
//self.navigationController = [[UINavigationController alloc]initWithRootViewController:rootView];
//4
self.window.rootViewController = self.navigationController;
//5
[self.window addSubview:self.navigationController.view];
//6
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window=[[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]autorelease];
self.window.backgroundColor=[UIColor whiteColor];
self.viewController= [[TestViewController alloc]init];
self.navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
returnYES;
}
之后就可以在对应的页面类中添加各种view组件了,如button,label,textfield,imageview,tableview,scrollview,或者以他们作为基类的第三方库,用库做开发还是很搞笑的,哦不对,是高效!!!
|
|
二、NavigationController中的一些Item
2-1NavigationController常用函数触发时机
- 当视图控制器的View将要出现时触发
- (void)viewWillAppear:(BOOL)animated
- 当视图控制器的View已经出现时触发
- (void)viewDidAppear:(BOOL)animated
- 当视图控制器的View将要消失时触发
- (void)viewWillDisappear:(BOOL)animated
- 当视图控制器的View已经消失时触发
- (void)viewDidDisappear:(BOOL)animated
当由前一个界面,push下一个界面时,如A push B,四个方法执行顺序
A(ViewWillDisappear)>B(viewWillAppear)>A(viewDidDisappear)>B(viewDidAppear)
当由后一个界面pop前一个界面时,如B pop A,四个方法执行顺序
B(viewWillDisappear)>A(viewWillAppear)>B(viewDidDisappear)>A(viewDidAppear)
2-2 NavigationBar常用属性
一. 对navigationBar直接配置,所以该操作对每一界面navigationBar上显示的内容都会有影响(效果是一样的)
1.修改navigationBar颜色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
2.关闭navigationBar的毛玻璃效果
self.navigationController.navigationBar.translucent = NO;
3.将navigationBar隐藏掉
self.navigationController.navigationBarHidden = YES;
4.给navigationBar设置图片
不同尺寸的图片效果不同:
1).320 * 44,只会给navigationBar附上图片
2).高度小于44,以及大于44且小于64:会平铺navigationBar以及状态条上显示
3).高度等于64:整个图片在navigationBar以及状态条上显示
[self.navigationController.navigationBarsetBackgroundImage[UIImageimageNamed:@"32050"]forBarMetrics:UIBarMetricsDefault];
二.navigationBar上显示的内容单独定制(每个界面显示内容不一样)
单独定制,修改视图控制器的navigationItem
1.设置界面的title
self.navigationItem.title = @"锰酸钾拌饭”;
2.修改titleView,添加个segment啥的,或者label
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"氧气", @"水"]];
self.navigationItem.titleView = segment;[segment release];
3.修改navigationBar的颜色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
4.修改navigationBar,右边显示内容
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonsystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(rightButton:)];
self.navigationItem.rightBarButtonItem = right;
5.leftBarButtonItem添加方法是一样的
因为都是之前在云笔记上整理好的,所以更新速度有点快,而且有些知识是做笔记时前辈写的东西,如有部分雷同,谢谢前辈!