概述
- 导航栏控制器和视图控制器关系
- 导航栏UI设置
- 涉及到添加自己的UIWindow的知识参考前面的iOS的UI-04-UIWindow
导航栏控制器和视图控制器关系
- 一个视图管理器对应一个页面(类似Android中Activity;Flutter中的route)
- 导航栏控制器管理视图控制器(一个个视图控制器做压栈操作,类似于Android中Activity的标准启动模式)
- 导航栏分三部分:左中右区域,都可以使用自定义view。也可以使用系统默认的。
- 导航栏控制器本身没有视图
看一张示意图
示例
图
示例代码
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain,nonatomic) UIWindow* window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "VCRoot.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//初始化window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//初始化视图控制器
VCRoot* root = [VCRoot new];
//初始化导航栏控制器
//导航栏控制器是用来管理多个视图控制器的
//多个视图控制器以压栈方式添加
//创建导航栏控制器时,一定要有一个根视图控制器
//p:根视图控制器
UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:root];
//设置导航栏控制器为根视图控制器
self.window.rootViewController = navC;
//生效并显示
[self.window makeKeyAndVisible];
return YES;
}
@end
VCRoot.m
#import "VCRoot.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//下面两种方式都能设置导航栏title
//self.title = @"title";
//self.navigationItem.title = @"第一页";
//中间位置可以是自定义view
UIView *view = [UIView new];
view.frame = CGRectMake(0, 0, 30, 30);
view.backgroundColor = [UIColor blueColor];
self.navigationItem.titleView = view;
//右侧位置可以使用系统自带的view,也可以使用自定义view。
//UIBarButtonItem是个容器类(包装类),有很多种初始化方法,通过内容的不同产生不同效果
//通过title初始化
//p1:文字内容
//p2:样式
//p3:点击函数实现目标类对象
//p4:点击函数
UIBarButtonItem* rightBtn01 = [[UIBarButtonItem alloc] initWithTitle:@"到第二页" style:UIBarButtonItemStyleDone target:self action:@selector(toSecond)];
//通过图片初始化(关键是切好图的尺寸)
//里面图片视图大小不能手动控制,只能通过控制图片尺寸控制
UIImage* image = [UIImage imageNamed:@"search.png"];
UIBarButtonItem* rightBtn02 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(search)];
//通过添加自定义的view初始化
UIImageView* iv = [[UIImageView alloc] initWithImage:image];
UIBarButtonItem* imageBtn = [[UIBarButtonItem alloc] initWithCustomView:iv];
imageBtn.width = 5;
NSArray* array = [NSArray arrayWithObjects:rightBtn01,rightBtn02,imageBtn, nil];
//右侧按钮项目,可以是单个按钮,也可以是一行几个按钮
//添加一个按钮
self.navigationItem.rightBarButtonItem = rightBtn01;
//self.navigationItem.rightBarButtonItem = rightBtn02;
//self.navigationItem.rightBarButtonItem = imageBtn;
//添加一行按钮
//self.navigationItem.rightBarButtonItems = array;
//右侧位置可以使用系统自带的view,也可以使用自定义view。
//通过系统style创建一个UIBarButtonItem,文字内容是系统规定的不能修改
UIBarButtonItem* leftBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(leftPress)];
//左侧按钮项目,可以是一个或者多个
self.navigationItem.leftBarButtonItem = leftBtn;
//作为非根视图控制器时,左上角返回按钮
//self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
//[self.navigationItem hidesBackButton];
self.view.backgroundColor = [UIColor redColor];
}
-(void) leftPress{
NSLog(@"left");
}
-(void) toSecond{
NSLog(@"toSecond");
}
-(void) search{
NSLog(@"search");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end