知識點: 1.UINavigationController 2.UINavigationItem 3.UINavigationBar 4.UINavigationController視圖切換 UINavigationController 1.什麼是導航控制器 作用:管理視圖控制器 2.UINavig ...
知識點:
1.UINavigationController
2.UINavigationItem
3.UINavigationBar
4.UINavigationController視圖切換
========================
UINavigationController
1.什麼是導航控制器
作用:管理視圖控制器
2.UINavigationController對象創建
1)初始化方式
- (id)initWithRootViewController:(UIViewController *)rootViewController
2)UINavigationController組成:
(1)navigationBar(高度44)
(2)customContent — 有自定義的ViewController提供
(3)navigationToolbar(高度44)
3.通過UINavigationController對象切換視圖
1)將視圖控制器壓入導航控制器的棧容器中
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated
2)將視圖控制器從導航控制器中彈出
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
========================
UINavigationItem
UINavigationItem包含了:
(1)backBarButtonItem(由上一級ctl的屬性決定)
(2)title/titleView(當前ctl)
(3)rightBarButtonItem(當前ctl)
(4)leftBarButtonItem(當前ctl)
1.UINavigationItem和UIViewController關係
navigationItem是UIViewController的一個屬性
這個屬性是為UINavigationController服務的
2.創建UIBarButtonItem
1)創建系統自帶的UIBarButtonSystemItem
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem
target:(id)target
action:(SEL)action;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(btnAction)];
2)文字UIBarButtonItem的創建方式
- (id)initWithTitle:(NSString *)title
style:(UIBarButtonItemStyle)style
target:(id)target
action:(SEL)action
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem new];
3)圖片UIBarButtonItem的創建方式
- (id)initWithImage:(UIImage *)image
style:(UIBarButtonItemStyle)style
target:(id)target
action:(SEL)action
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"refresh_30"] style:UIBarButtonItemStylePlain target:nil action:nil];
3)如何將UIBarButtonItem加入到導航的左邊和右邊
@property (nonatomic, retain) UIBarButtonItem *leftBarButtonItem
@property (nonatomic, retain) UIBarButtonItem *rightBarButtonItem
@property (nonatomic, retain) UIBarButtonItem *backBarButtonItem
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:b];
3.定製UIBarButtonItem
4.定製導航中間的titleView
self.navigationItem.title = @"控制器2";
5.定製backBarButtonItem
註意:設置當前控制器的backBarButtonItem需要在下一個控制器中才能顯示
========================
UINavigationBar
1.如何往UINavigationBar貼圖
設置背景圖片
- (void)setBackgroundImage:(UIImage *)backgroundImage
forBarMetrics:(UIBarMetrics)barMetrics
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.png"] forBarMetrics:UIBarMetricsDefault];
2.如何設置UINavigationBar設置顏色
@property (nonatomic, retain) UIColor *tintColor
@property (nonatomic, retain) UIColor *barTintColor
3.如何設置透明顏色
1)設置UINavigationBar的樣式
@property (nonatomic, assign) UIBarStyle barStyle
self.navigationController.navigationBar.barStyle= UIBarStyleBlack;
2)是否透明
@property (nonatomic, assign, getter=isTranslucent) BOOL translucent
//獲取管理當前視圖控制器的導航控制器(如果這個視圖控制器沒有受到導航控制器管理,此方法會返回空指針)
//設置導航欄為不透明,坐標點會自動下移64個單位
self.navigationController.navigationBar.translucent = NO;
3)改變導航欄的顏色
@property(nonatomic,retain) UIColor *barTintColor
//設置背景色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
4.如何隱藏UINavigationBar
1)不帶動畫隱藏
@property (nonatomic, getter=isNavigationBarHidden) BOOL navigationBarHidden
//顯示
self.navigationController.navigationBarHidden = NO;
2)帶動畫隱藏
- (void) setNavigationBarHidden:(BOOL)navigationBarHidden
animated:(BOOL)animated
[self.navigationController setNavigationBarHidden:YES animated:YES];
========================
UINavigationController視圖切換
1.獲取導航控制器中的視圖數組
@property (nonatomic, copy) NSArray *viewControllers
2.將視圖控制器壓入導航控制器的棧容器中
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated
//視圖控制器入棧
[self.navigationController pushViewController:ctlA animated:YES];
3.將視圖控制器從導航控制器中彈出
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
4.切換至指定的視圖控制器(該控制器必須在當前導航控制器中的棧中)
- (NSArray *)popToViewController:(UIViewController *)viewController
animated:(BOOL)animated
5.回到根視圖控制器
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated