建議看此文之前,看如下文章
UINavigaiongController的基本使用
UINavigaiongController實(shí)現(xiàn)控制器直接的切換
**層次結(jié)構(gòu) **
@interface UINavigationController : UIViewController
@interface UIViewController (UINavigationControllerItem)
@interface UINavigationItem : NSObject <NSCoding>
@interface UISwitch : UIControl <NSCoding>
@interface UIControl : UIView
@interface UIBarButtonItem : UIBarItem <NSCoding>
@interface UIBarItem : NSObject <NSCoding, UIAppearance>
//UINavigationController的方法和屬性
@property(nonatomic,readonly,strong) UINavigationItem *navigationItem;
//UINavigationItem的方法和屬性
@property(nullable, nonatomic,strong) UIView *titleView;
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@property(nullable,nonatomic,copy)
NSArray<UIBarButtonItem *> *leftBarButtonItems
// UIBarButtonItem 的方法和屬性
- (instancetype)initWithTitle:(nullable NSString *)title style
(UIBarButtonItemStyle)style target:
(nullable id)target action:(nullable SEL)action;
- (instancetype)initWithBarButtonSystemItem
(UIBarButtonSystemItem)systemItem target:
(nullable id)target action:(nullable SEL)action;
//系統(tǒng)的按鈕,枚舉
typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {. . .}
代碼
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建返回按鈕
//當(dāng)'返回按鈕'和 title 同時(shí)設(shè)置時(shí),棧頂界面,只顯示'返回按鈕'
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:
@"返回按鈕" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
//創(chuàng)建中間標(biāo)題文字
//self.navigationItem.title = @"這是個(gè)標(biāo)題";
//創(chuàng)建中間標(biāo)題視圖
//self.navigationItem.titleView = [[UISwitch alloc]init];
//創(chuàng)建左側(cè)按鈕
UIBarButtonItem *lefItem2 = [[UIBarButtonItem alloc]initWithTitle:
@"左側(cè)按鈕" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftBarButtonItem = lefItem2;
//創(chuàng)建系統(tǒng)的按鈕
//target - 目標(biāo)
//action - 事件
UIBarButtonItem *lefItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem : UIBarButtonSystemItemDone target:self
action:@selector(hehe)];
self.navigationItem.leftBarButtonItem = lefItem;
//創(chuàng)建左側(cè),多個(gè)按鈕
self.navigationItem.leftBarButtonItems = @[lefItem,lefItem2];
//創(chuàng)建右側(cè)按鈕
//target: 目標(biāo)
//action: 事件
UIBarButtonItem *rightItem =
[[UIBarButtonItem alloc]initWithTitle:@"右側(cè)按鈕"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightItem;
UIBarButtonItem *rightItem2 =
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:
UIBarButtonSystemItemSearch target:self action:@selector(hehe)];
//創(chuàng)建右側(cè)多個(gè)按鈕
self.navigationItem.rightBarButtonItems =@[rightItem,rightItem2];
}
效果圖
返回按鈕
中間標(biāo)題內(nèi)容
中間標(biāo)題視圖
左側(cè)按鈕
系統(tǒng)的按鈕
注意點(diǎn)
導(dǎo)航欄的內(nèi)容,由棧頂控制器的navigationItem屬性決定!