1.自定義UIViewController
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//當(dāng)程序完成硬件加載的工作之后,就會(huì)來(lái)回調(diào)這個(gè)方法
//通過(guò)這個(gè)方法來(lái)配置加載哪個(gè)界面
//默認(rèn) 系統(tǒng)會(huì)加載Main.storyboard里面的第一個(gè)界面作為主界面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//定義程序的界面加載 加載哪個(gè)界面
//配置界面的加載
//1.創(chuàng)建一個(gè)窗口
self.window = [[UIWindow alloc] init];
//設(shè)置窗口的大小 和屏幕大小一樣
_window.frame = [UIScreen mainScreen].bounds;
//創(chuàng)建主界面
MainViewController *mainVC = [[MainViewController alloc] init];
//設(shè)置主界面的背景顏色為白色
mainVC.view.backgroundColor = [UIColor whiteColor];
//創(chuàng)建一個(gè)導(dǎo)航欄控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
//將mainVC設(shè)置為窗口的root view controller
_window.rootViewController = nav;
//顯示窗口
[self.window makeKeyAndVisible];
return YES;
}
@end
刪除原本的UIViewController,重新創(chuàng)建,在AppDelegate中重新創(chuàng)建。在創(chuàng)建過(guò)程中可以設(shè)置導(dǎo)航欄。
導(dǎo)航欄控制器
UIViewController 控制一個(gè)界面 管理一個(gè)界面的一切(控件的顯示 事件的傳遞)
UIView 一個(gè)視圖一個(gè)矩形的顯示區(qū)域 如何顯示一個(gè)視圖和視圖的基本操作
UINavigationController 管理界面之間的切換
講解圖
通過(guò)push 推送到下一個(gè)界面 當(dāng)前這個(gè)界面會(huì)被壓棧
通過(guò)pop返回到上一個(gè)界面 當(dāng)前這個(gè)界面會(huì)被刪除 摧毀
window -> UINavigationController ->viewController
在storyboard 快速添加一個(gè)導(dǎo)航欄控制器
使用embed in 快速插入一個(gè)導(dǎo)航欄控制器
找到 UINavigationController 設(shè)置
示例圖
更改導(dǎo)航欄背景
- 更改導(dǎo)航欄的背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
- 更改導(dǎo)航欄的背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"圖片名"] forBarMetrics:UIBarMetricsDefault];
添加按鈕 UINavigationItem left right 中間的圖標(biāo)
- 創(chuàng)建一個(gè)有標(biāo)題的UIBarButtonItem---左邊的
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = back;
- 創(chuàng)建一個(gè)有圖片UIBarButtonItem---右邊的
UIImage *img = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(next)];
self.navigationItem.rightBarButtonItem = next;
- 自定義一個(gè)按鈕添加到導(dǎo)航欄的右邊
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 60, 35);
[btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
//創(chuàng)建UIBarButtonItem
UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = nextBtn;
- 中間視圖 -> 顯示標(biāo)題
self.title = @"標(biāo)題";
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:17],NSForegroundColorAttributeName:[UIColor whiteColor]};
- 中間視圖 -> 顯示圖片
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 32, 120, 19)];
logo.image = [UIImage imageNamed:@""];
self.navigationItem.titleView = logo;
工具條UIToolbar
//默認(rèn)工具條UIToolbar是隱藏的
self.navigationController.toolbarHidden = NO;
//工具欄的風(fēng)格
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
typedef enum {
UIBarStyleDefault, //默認(rèn)風(fēng)格;灰色背景、白色文字
UIBarStyleBlack,
UIBarStyleBlackOpaque, //純黑色背景、白色文字
UIBarStyleBlackTranslucent //透明黑色背景、白色文字
} UIBarStyle;
//添加按鈕 UIBarButtonItem
//創(chuàng)建用于均分toolBar的barButtonItem
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *copyBtn = [[UIBarButtonItem alloc] initWithTitle:@"Copy" style:UIBarButtonItemStylePlain target:nil action:nil];
//將按鈕添加到toolBar上
self.toolbarItems = @[flexible,copyBtn,flexible,copyBtn,flexible];
系統(tǒng)提供的小型按鈕庫(kù)
typedef enum {
UIBarButtonSystemItemDone, //藍(lán)色文字按鈕,標(biāo)有“Done”
UIBarButtonSystemItemCancel, //文字按鈕,標(biāo)有“Cancel”
UIBarButtonSystemItemEdit, //文字按鈕,標(biāo)有“Edit”
UIBarButtonSystemItemSave, //藍(lán)色文字按鈕,標(biāo)有“Save”
UIBarButtonSystemItemAdd, //圖像按鈕,上面有一個(gè)?符號(hào)
UIBarButtonSystemItemFlexibleSpace, //空白,占用空間大小可變
UIBarButtonSystemItemFixedSpace, //空白占位符
UIBarButtonSystemItemCompose, //圖像按鈕,上有一支筆和紙張
UIBarButtonSystemItemReply, //圖像按鈕,上有一個(gè)回復(fù)箭頭
UIBarButtonSystemItemAction, //圖像按鈕,上有一個(gè)動(dòng)作箭頭
UIBarButtonSystemItemOrganize, //圖像按鈕,上有一個(gè)文件夾以及向下箭頭
UIBarButtonSystemItemBookmarks, //圖像按鈕,上有書(shū)簽圖標(biāo)
UIBarButtonSystemItemSearch, //圖像按鈕,上有spotlight圖標(biāo)
UIBarButtonSystemItemRefresh, //圖像按鈕,上有一個(gè)環(huán)形的刷新箭頭
UIBarButtonSystemItemStop, //圖像按鈕,上有一個(gè)停止記號(hào)X
UIBarButtonSystemItemCamera, //圖像按鈕,上有一個(gè)照相機(jī)
UIBarButtonSystemItemTrash, //圖像按鈕,上有一個(gè)垃圾桶
UIBarButtonSystemItemPlay, //圖像按鈕,上有一個(gè)播放圖標(biāo)
UIBarButtonSystemItemPause, //圖像按鈕,上有一個(gè)暫停圖標(biāo)
UIBarButtonSystemItemRewind, //圖像按鈕,上有一個(gè)倒退圖標(biāo)
UIBarButtonSystemItemFastForward, //圖像按鈕,上有一個(gè)快進(jìn)圖標(biāo)
UIBarButtonSystemItemUndo , //Undo
UIBarButtonSystemItemRedo , //Redo
UIBarButtonSystemItemPageCurl
} UIBarButtonSystemItem;
1
2
3
4
代碼例子
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//更改導(dǎo)航欄的背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//更改導(dǎo)航欄的背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
//添加按鈕 UINavigationItem left right 中間的圖標(biāo)
//創(chuàng)建一個(gè)有標(biāo)題的UIBarButtonItem---左邊的
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = back;
//創(chuàng)建一個(gè)有圖片UIBarButtonItem---右邊的
UIImage *img = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(next)];
self.navigationItem.rightBarButtonItem = next;
//自定義一個(gè)按鈕添加到導(dǎo)航欄的右邊
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 60, 35);
[btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
//創(chuàng)建UIBarButtonItem
//UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
//self.navigationItem.rightBarButtonItem = nextBtn;
//中間視圖 -> 顯示標(biāo)題
self.title = @"標(biāo)題";
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:17],NSForegroundColorAttributeName:[UIColor whiteColor]};
//中間視圖 -> 顯示圖片
//UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 32, 120, 19)];
//logo.image = [UIImage imageNamed:@""];
//self.navigationItem.titleView = logo;
//默認(rèn)工具條UIToolbar是隱藏的
self.navigationController.toolbarHidden = NO;
//工具欄的風(fēng)格
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
//添加按鈕 UIBarButtonItem
//創(chuàng)建用于均分toolBar的barButtonItem
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *copyBtn = [[UIBarButtonItem alloc] initWithTitle:@"Copy" style:UIBarButtonItemStylePlain target:nil action:nil];
//將按鈕添加到toolBar上
self.toolbarItems = @[flexible,copyBtn,flexible,copyBtn,flexible];
}
-(void)back{
NSLog(@"here");
}
-(void)next{
NSLog(@"there");
}
@end
運(yùn)行結(jié)果