开头
- 这是分篇章,整篇请看零基础iOS开发学习日记
自定义根控制器
- xcode生成的项目默认是加载
storyboard
的,而我现在一般喜欢纯代码开发,所以要进行项目的初始设置
- 删掉
SceneDelegate.h/.m
AppDelegate.h
中添加@property (nonatomic, strong) UIWindow *window;
Info.plist
删掉有Scene
设置,删掉有Main
的值- 删掉
Main.storyboard
AppDelegate.m
实现下面这个函数,可以理解为App的入口,app的一些初始化设置,都可以写在这个函数里
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置根控制器
ViewController *vc = [ViewController new];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
复制代码
UINavigationController
实际用处
- 管理有关联的页面
基础用法
UINavigationController
实际上就是个盒子,设置了根控制器后,从这个根控制器跳转的都会从里到外,放在这个盒子里。- 设置app的根控制器,并将
ViewController
放入NavigationController
中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [ViewController new];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
复制代码
ViewController
跳转到testViewController
,testViewController
也放在这个盒子里
- (void)btnClick {
testViewController *vc = [testViewController new];
[self.navigationController pushViewController:vc animated:YES];
}
复制代码
- 界面弹出
//弹回上一个界面,如果是第一页无效
[self.navigationController popViewControllerAnimated:YES];
//弹到指定的界面,这个界面必须要在这个盒子中,而不能新建
ViewController *vc = self.navigationController.viewControllers[0];
[self.navigationController popToViewController:vc animated:YES];
复制代码
UINavigationBar
UINavigationBar
是添加UINavigationController
后,状态栏显示的导航栏- 主要由三部分组成,
leftBarButton
、rightBarButton
、title
。在实际开发中最常用的地方,也是设置左上角与右上角的按钮,和中间的标题
基本按钮添加
//标题
self.navigationItem.title = @"首页";
//系统样式按钮
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(cameraClick)];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playClick)];
self.navigationItem.rightBarButtonItem = right;
self.navigationItem.leftBarButtonItem = left;
//自定义文字按钮
UIBarButtonItem *helpItem = [[UIBarButtonItem alloc]initWithTitle:@"常见问题" style:UIBarButtonItemStylePlain target:self action:@selector(helpItemClick)];
self.navigationItem.rightBarButtonItem = helpItem;
//自定义图片按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
self.navigationItem.leftBarButtonItem = item;
复制代码
- 以上,有几点要注意
- 如果左右同时添加一个
UIBarButtonItem
,则只显示右边 - 用
self.navigationItem.rightBarButtonItem = helpItem
这类方式添加,只会显示最左或者最右的按钮
添加多个按钮
- 添加多个按钮,会自动布局,并将标题挤过去
UIBarButtonItem *helpItem = [[UIBarButtonItem alloc]initWithTitle:@"常见问题" style:UIBarButtonItemStylePlain target:self action:@selector(helpItemClick)];
UIBarButtonItem *left1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClick)];
UIBarButtonItem *left2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playClick)];
UIBarButtonItem *left3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashClick)];
self.navigationItem.leftBarButtonItems = @[left1, left2, left3, helpItem];
复制代码
自定义Button添加
- 自定义控件作为
UIBarButtonItem
,这里针对的情况是,按钮图片大小很大,而且需要另外修改约束,特别注意UIImage
的渲染模式的设置
UIImage *image = [UIImage imageNamed:@"dv_icon1"];
//原图渲染
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[btn setImage:image forState:UIControlStateNormal];
//修改约束
CGFloat width = 44 * image.size.width / image.size.height;
[btn.widthAnchor constraintEqualToConstant:width].active = YES;
[btn.heightAnchor constraintEqualToConstant:44].active = YES;
[btn addTarget:self action:@selector(cameraClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item;
复制代码
修改外观
UINavigationBar
默认的渲染颜色是蓝色AppDelegate
中设置
//渲染颜色
[[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];
//标题样式
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor], NSFontAttributeName : [UIFont systemFontOfSize:30]}];
//标题背景
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
复制代码
- 自定义
UINavigationController
设置
//设置导航栏背景
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
//设置标题字体
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor], NSFontAttributeName : [UIFont systemFontOfSize:30]}];
//设置渲染的颜色
[self.navigationBar setTintColor:[UIColor blackColor]];
复制代码
内部跳转
- 内部跳转指的是,经过
UINavigationController
跳转后的界面,都会被保存在viewControllers
的数组中 - 其中销毁过程,也是按照上面所讲从下往上放的顺序,从上往下销毁,如果中间有某个界面被销毁后,这个界面上的所有界面也会被销毁,销毁顺序为从下往上,例如,界面顺序从下往上依次为
红绿蓝
,一旦从蓝
直接跳到红
,会先销毁绿
,在销毁蓝
//拿到导航控制器栈中的所有控制器
NSArray *vcs = self.navigationController.viewControllers;
UIViewController *vc = vcs[1];
[self.navigationController popToViewController:vc animated:YES];
复制代码
设置状态栏的样式
//颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
//隐藏
- (BOOL)prefersStatusBarHidden {
return YES;
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END