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

1.自定義UIViewController

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

@interface AppDelegate ()

@end

@implementation AppDelegate


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

@end

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

導航欄控制器

UIViewController 控制一個界面 管理一個界面的一切(控件的顯示 事件的傳遞)
UIView 一個視圖一個矩形的顯示區域 如何顯示一個視圖和視圖的基本操作
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 中間的圖標

  • 創建一個有標題的UIBarButtonItem---左邊的
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = back;
  • 創建一個有圖片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];
//創建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
//創建用于均分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];

系統提供的小型按鈕庫

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

    UIBarButtonSystemItemAdd,            //圖像按鈕,上面有一個?符號
    UIBarButtonSystemItemFlexibleSpace,  //空白,占用空間大小可變
    UIBarButtonSystemItemFixedSpace,     //空白占位符
    UIBarButtonSystemItemCompose,        //圖像按鈕,上有一支筆和紙張
    UIBarButtonSystemItemReply,          //圖像按鈕,上有一個回復箭頭
    UIBarButtonSystemItemAction,         //圖像按鈕,上有一個動作箭頭
    UIBarButtonSystemItemOrganize,       //圖像按鈕,上有一個文件夾以及向下箭頭
    UIBarButtonSystemItemBookmarks,      //圖像按鈕,上有書簽圖標
    UIBarButtonSystemItemSearch,         //圖像按鈕,上有spotlight圖標
    UIBarButtonSystemItemRefresh,        //圖像按鈕,上有一個環形的刷新箭頭
    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 中間的圖標
    
    //創建一個有標題的UIBarButtonItem---左邊的
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = back;
    //創建一個有圖片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];
    //創建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
    //創建用于均分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
運行結果
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容