問題解決1:導航欄/工具欄的搭建以及設置

1.自定義UIViewController

#import "AppDelegate.h"
#import "MainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


//當程序完成硬件加載的工作之后,就會來回調(diào)這個方法
//通過這個方法來配置加載哪個界面
//默認 系統(tǒng)會加載Main.storyboard里面的第一個界面作為主界面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //定義程序的界面加載 加載哪個界面
    //配置界面的加載
    //1.創(chuàng)建一個窗口
    self.window = [[UIWindow alloc] init];
    
    //設置窗口的大小 和屏幕大小一樣
    _window.frame = [UIScreen mainScreen].bounds;
    
    //創(chuàng)建主界面
    MainViewController *mainVC = [[MainViewController alloc] init];
    //設置主界面的背景顏色為白色
    mainVC.view.backgroundColor = [UIColor whiteColor];
    
    //創(chuàng)建一個導航欄控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
    
    //將mainVC設置為窗口的root view controller
    _window.rootViewController = nav;
    
    //顯示窗口
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end

刪除原本的UIViewController,重新創(chuàng)建,在AppDelegate中重新創(chuàng)建。在創(chuàng)建過程中可以設置導航欄。

導航欄控制器

UIViewController 控制一個界面 管理一個界面的一切(控件的顯示 事件的傳遞)
UIView 一個視圖一個矩形的顯示區(qū)域 如何顯示一個視圖和視圖的基本操作
UINavigationController 管理界面之間的切換

講解圖

通過push 推送到下一個界面 當前這個界面會被壓棧
通過pop返回到上一個界面 當前這個界面會被刪除 摧毀

window -> UINavigationController ->viewController

在storyboard 快速添加一個導航欄控制器

使用embed in 快速插入一個導航欄控制器
找到 UINavigationController 設置

示例圖

更改導航欄背景

  • 更改導航欄的背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
  • 更改導航欄的背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"圖片名"]  forBarMetrics:UIBarMetricsDefault];

添加按鈕 UINavigationItem left right 中間的圖標

  • 創(chuàng)建一個有標題的UIBarButtonItem---左邊的
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = back;
  • 創(chuàng)建一個有圖片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;
  • 自定義一個按鈕添加到導航欄的右邊
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;
  • 中間視圖 -> 顯示標題
self.title = @"標題";
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

//默認工具條UIToolbar是隱藏的
self.navigationController.toolbarHidden = NO;
//工具欄的風格
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

typedef enum {
    UIBarStyleDefault,         //默認風格;灰色背景、白色文字
    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)提供的小型按鈕庫

typedef enum {
    UIBarButtonSystemItemDone,           //藍色文字按鈕,標有“Done”
    UIBarButtonSystemItemCancel,         //文字按鈕,標有“Cancel”
    UIBarButtonSystemItemEdit,           //文字按鈕,標有“Edit”
    UIBarButtonSystemItemSave,           //藍色文字按鈕,標有“Save”

    UIBarButtonSystemItemAdd,            //圖像按鈕,上面有一個?符號
    UIBarButtonSystemItemFlexibleSpace,  //空白,占用空間大小可變
    UIBarButtonSystemItemFixedSpace,     //空白占位符
    UIBarButtonSystemItemCompose,        //圖像按鈕,上有一支筆和紙張
    UIBarButtonSystemItemReply,          //圖像按鈕,上有一個回復箭頭
    UIBarButtonSystemItemAction,         //圖像按鈕,上有一個動作箭頭
    UIBarButtonSystemItemOrganize,       //圖像按鈕,上有一個文件夾以及向下箭頭
    UIBarButtonSystemItemBookmarks,      //圖像按鈕,上有書簽圖標
    UIBarButtonSystemItemSearch,         //圖像按鈕,上有spotlight圖標
    UIBarButtonSystemItemRefresh,        //圖像按鈕,上有一個環(huán)形的刷新箭頭
    UIBarButtonSystemItemStop,           //圖像按鈕,上有一個停止記號X
    UIBarButtonSystemItemCamera,         //圖像按鈕,上有一個照相機
    UIBarButtonSystemItemTrash,          //圖像按鈕,上有一個垃圾桶
    UIBarButtonSystemItemPlay,           //圖像按鈕,上有一個播放圖標
    UIBarButtonSystemItemPause,          //圖像按鈕,上有一個暫停圖標
    UIBarButtonSystemItemRewind,         //圖像按鈕,上有一個倒退圖標
    UIBarButtonSystemItemFastForward,    //圖像按鈕,上有一個快進圖標
    UIBarButtonSystemItemUndo ,          //Undo
    UIBarButtonSystemItemRedo ,          //Redo
    UIBarButtonSystemItemPageCurl        
} UIBarButtonSystemItem;
1
2
3
4

代碼例子

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //更改導航欄的背景顏色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    //更改導航欄的背景圖片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    
    //添加按鈕 UINavigationItem left right 中間的圖標
    
    //創(chuàng)建一個有標題的UIBarButtonItem---左邊的
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = back;
    //創(chuàng)建一個有圖片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;
    //自定義一個按鈕添加到導航欄的右邊
    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;
    //中間視圖 -> 顯示標題
    self.title = @"標題";
    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是隱藏的
    self.navigationController.toolbarHidden = NO;
    //工具欄的風格
    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
運行結果
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,505評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,556評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,463評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,009評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,778評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,218評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,281評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,436評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,969評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,795評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,993評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,537評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,229評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,659評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,917評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,687評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,990評論 2 374

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