問(wèn)題解決1:導(dǎo)航欄/工具欄的搭建以及設(shè)置

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é)果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,143評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 175,553評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 62,620評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,416評(píng)論 6 405
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 54,940評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,170評(píng)論 0 287
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,709評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,597評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,784評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評(píng)論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,029評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 34,407評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 35,663評(píng)論 1 280
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,403評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,746評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容