iOS開發-UI (六)Navigation,ios-ui
知識點:
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
//擷取管理當前視圖控制器的導航控制器(如果這個視圖控制器沒有受到導航控制器管理,此方法會返回null 指標)
//設定導覽列為不透明,座標點會自動下移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