问题解决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 管理界面之间的切换

问题解决1:导航栏/工具栏的搭建以及设置_第1张图片
讲解图

通过push 推送到下一个界面 当前这个界面会被压栈
通过pop返回到上一个界面 当前这个界面会被删除 摧毁

window -> UINavigationController ->viewController

在storyboard 快速添加一个导航栏控制器

使用embed in 快速插入一个导航栏控制器
找到 UINavigationController 设置

问题解决1:导航栏/工具栏的搭建以及设置_第2张图片
示例图

更改导航栏背景

  • 更改导航栏的背景颜色
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:导航栏/工具栏的搭建以及设置_第3张图片
1
问题解决1:导航栏/工具栏的搭建以及设置_第4张图片
2
问题解决1:导航栏/工具栏的搭建以及设置_第5张图片
3
问题解决1:导航栏/工具栏的搭建以及设置_第6张图片
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
问题解决1:导航栏/工具栏的搭建以及设置_第7张图片
运行结果

你可能感兴趣的:(问题解决1:导航栏/工具栏的搭建以及设置)