1,UINavigationController簡介
導航控制器,ios常用
使用堆棧存儲。根視圖控制器在堆棧最底層。
[self.navigationController pushViewController:viewController animated:YES]; //入棧 進入viewController視圖控制器
[self.navigationController popViewControllerAnimated:YES]; //出棧,返回上一個視圖控制器
[self.navigationController popToViewController:UIViewController animated:YES]; //返回到指定的試圖控制器
[self.navigationController popToRootViewControllerAnimated:YES]; //返回根控制器
2,UINavigationController的結構組成
UINavigationController由Navigation bar ,Navigation View ,Navigation toobar等組成;
3,UINavigationController的創建
UINavigationController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor groupTableViewBackgroundColor];
ViewController *rootVC = [[ViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController: rootVC];
[self.window setRootViewController: navigation];
[self.window makeKeyAndVisible];
return YES;
}
4,UINavigationBar
(1)默認NavigationBar
navigationBar--導航條,iOS7以後默認是透明的,iOS7以前默認是不透明的。
navigationBar在透明情況下,與contentView會重合一部分區域
navigationBar在不透明情況,ContentView跟在navigationBar下面
navigationBar豎屏下默認高度44,橫屏下默認高度32
(2)自定義NavigationBar
[self.navigationController.navigationBar setTranslucent: NO]; //顯示出NavigationBar
self.navigationController.navigationBar.backgroundColor = [UIColor redColor]; // 設置導航條的顏色
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tupianName"] forBarMetrics:UIBarMetricsCompact]; //導航條加背景圖片
(3)自定義左側按鈕
- (void)viewDidLoad {
[super viewDidLoad];
// 自定義導航欄左側按鈕
UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
leftBtn.frame = CGRectMake(0, 10, 93, 30);
leftBtn.backgroundColor = [UIColor orangeColor];
[leftBtn addTarget:self action:@selector(onTap) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftItem;
}
(4)自定義右側按鈕
與左側按鈕類似,區別在於:
self.navigationItem.rightBarButtonItem = rightItem;
(5)自定義中間視圖
UIView * centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 120, 30)];
centerView.backgroundColor = [UIColor greenColor];
self.navigationItem.titleView = centerView;
5,UINavigationToolbar
(1)顯示Toolbar
[self.navigationController setToolbarHidden:NO animated:YES] ;
(2)在ToolBar上添加UIBarButtonItem
UIBarButtonItem *camera=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(ClickToolBarButton)];
[camera setWidth:80];
UIBarButtonItem *refresh=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(ClickToolBarButton)];
[refresh setWidth:80];
UIBarButtonItem *reply=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(ClickToolBarButton)];
[reply setWidth:80];
UIBarButtonItem *compose=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(ClickToolBarButton)];
[compose setWidth:80];
UIBarButtonItem *splitspace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObjects:splitspace,camera,splitspace,refresh,splitspace,reply,splitspace,compose,splitspace, nil nil]];