UINavigationController是IOS編程中比較常用的一種viewcontroller,在介紹它的功能之前函荣,我們先對(duì)比一下是否使用UINavigationController,在界面上有什么異同:
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:[MainViewController new]];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
//使用navigationController
// self.window.rootViewController = navigationController;
//不使用navigationController
self.window.rootViewController = [EmptyViewController new];
根據(jù)以上代碼只损,我們可以看到如下的運(yùn)行結(jié)果:
根據(jù)上圖對(duì)比可知塌衰,使用navigationController比普通的viewcontroller多了上面一層導(dǎo)航條阀湿,可以更方便的控制界面的跳轉(zhuǎn)兔仰。
UINavigationController view層級(jí)
介紹UINavigationController view層級(jí),我們先從一張圖開始:
UINavigationController也是一個(gè)ViewController破停,并不僅僅是一個(gè)導(dǎo)航條翅楼,它是在普通的ViewController基礎(chǔ)上增加了新的功能。根據(jù)上面的圖真慢,我們進(jìn)行逐步的分析毅臊。
UINavigationItem
我們都知道navigationItem是UIViewController的一個(gè)屬性,這個(gè)屬性是為UINavigationController服務(wù)的晤碘。
查看源碼褂微,UINavigationItem中有兩個(gè)比較常用的item:
// Some navigation items want to display a custom left or right item when they're on top of the stack.
// A custom left item replaces the regular back button unless you set leftItemsSupplementBackButton to YES
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
也就是導(dǎo)航欄上的左右button,他們都是navigationItem中的一個(gè)組件园爷,我們可以直接設(shè)置宠蚂,例如:
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"Root"
style:UIBarButtonSystemItemAction
target:self
action:@selector(butClick)];
self.navigationItem.rightBarButtonItem = rightItem;
self.navigationItem.title = @"Hello, im the title";
運(yùn)行結(jié)果如下:
UINavigationItem繼承自NSObject,包含了當(dāng)前頁面導(dǎo)航欄上需要顯示的全部信息:title,prompt,titleView,leftBarButtonItem,rightBarButtonItem,backBarButonItem
UINavigationBar
UINavigationBar繼承自UIView童社,看一下他的屬性可知求厕,他主要是對(duì)UINavigationItem進(jìn)行管理的。
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
// Pushing a navigation item displays the item's title in the center of the navigation bar.
// The previous top navigation item (if it exists) is displayed as a "back" button on the left.
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; // Returns the item that was popped.
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
........
UINavigationBar與UINavigationItem的區(qū)別
- UINavigationBar是繼承自UIView的扰楼,UINavigationItem繼承自NSObject的呀癣。各自的屬性和功能不同
- UINavigationBar并包含所有UINavigationItem的棧,管理整個(gè)NavigationController的UINavigationItem。