问题解决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
运行结果
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容