一. UINavigationController的基本使用
状态栏高度一般是20
导航栏高度一般是44
导航栏视图控制器是堆栈视图结构,进入新的页面,原来的页面不会被销毁,而是压到下面
1. 导航栏视图控制器常见属性
//
// ViewController.m
// NavigationController
// 1 每次跳转的页面都是一个单独的视图控制器
// 2 UINavigationController = 导航 + 视图控制器 = 导航 + UIView + Controller
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 导航栏视图控制器的一些常用属性
// 1 title 导航栏控制器名称
// 2 navigationItem 导航栏
// 3 titleView
// 4 UIButton
self.navigationItem.title = @"导航栏";
self.title = @"导航栏1";//这个和上面的效果一样,但是修改的地方不一样,self.title修改的是当前视图控制器的title,而self.navigationItem.title修改的是视图控制器中承载的navigationItem.title
// 默认titleView是居中的,即便设置为0,0,也是在中间,即xy坐标不起作用
self.navigationItem.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
self.navigationItem.titleView.backgroundColor = [UIColor redColor];
UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
[bt setTitle:@"Color" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem.titleView addSubview:bt];
}
- (void)buttonAction {
self.view.backgroundColor = [UIColor grayColor];
}
@end
2. 创建一个导航栏视图控制器,在导航栏的titleView处添加一个圆形的头像
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Home_refresh_bg.png"]];
imageView.frame = CGRectMake(30, 0, 40, 40);
imageView.layer.cornerRadius = 20.0;
imageView.layer.masksToBounds = YES;
self.navigationItem.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
[self.navigationItem.titleView addSubview:imageView];
3. 导航栏视图控制器Push和Pop方法
Push: 产生一个新的页面
Pop: 返回的旧的页面
//
// ViewController.m
// NavigationController
// 1 每次跳转的页面都是一个单独的视图控制器
// 2 UINavigationController = 导航 + 视图控制器 = 导航 + UIView + Controller
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface ViewController () {
FirstViewController *firstVC;
SecondViewController *secondVC;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 导航栏视图控制器的一些常用属性
// 1 title 导航栏控制器名称
// 2 navigationItem 导航栏
// 3 titleView
// 4 UIButton
/*
self.navigationItem.title = @"导航栏";
self.title = @"导航栏1";//这个和上面的效果一样,但是修改的地方不一样,self.title修改的是当前视图控制器的title,而self.navigationItem.title修改的是视图控制器中承载的navigationItem.title
// 默认titleView是居中的,即便设置为0,0,也是在中间,即xy坐标不起作用
self.navigationItem.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
self.navigationItem.titleView.backgroundColor = [UIColor redColor];
UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
[bt setTitle:@"Color" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem.titleView addSubview:bt];
*/
//push 0 -> 1
//push 0 -> 2
//push 1 -> 2
//pop 1 -> 0
//pop 2 -> 0
//pop 可以直接通过左上角返回
//补充 self.navigationController.viewControllers返回一个数组,是堆栈系统中所有已经进栈的视图控制器
//补充 self.navigationController.topViewController
//补充 self.navigationController.visibleViewController 获取到当前界面显示的视图控制器
self.navigationItem.title = @"Main0";
self.view.backgroundColor = [UIColor grayColor];
UIButton *bt1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 60)];
[bt1 setTitle:@"Push" forState:UIControlStateNormal];
[bt1 addTarget:self action:@selector(buttonAction1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt1];
UIButton *bt2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 60)];
[bt2 setTitle:@"Push0->2" forState:UIControlStateNormal];
[bt2 addTarget:self action:@selector(buttonAction2 ) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt2];
self.view.tag = 10000;
}
- (void)buttonAction1 {
if (firstVC == nil) {
firstVC = [[FirstViewController alloc]init];
}
[self.navigationController pushViewController:firstVC animated:true];
}
- (void)buttonAction2 {
if (secondVC == nil) {
secondVC = [[SecondViewController alloc]init];
}
[self.navigationController pushViewController:secondVC animated:true];
}
- (void)buttonAction {
self.view.backgroundColor = [UIColor grayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// FirstViewController.m
// NavigationController
//
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ViewController.h"
@interface FirstViewController () {
SecondViewController *secondVC;
ViewController *vc;
}
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"First1";
self.view.backgroundColor = [UIColor redColor];
UIButton *bt1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 60)];
[bt1 setTitle:@"Push1" forState:UIControlStateNormal];
[bt1 addTarget:self action:@selector(buttonAction1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt1];
UIButton *bt2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 60)];
[bt2 setTitle:@"Pop1->0" forState:UIControlStateNormal];
[bt2 addTarget:self action:@selector(buttonAction2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt2];
}
- (void)buttonAction1 {
if (secondVC == nil) {
secondVC = [[SecondViewController alloc]init];
}
[self.navigationController pushViewController:secondVC animated:true];
}
- (void)buttonAction2 {
if (vc == nil) {
vc = [[ViewController alloc]init];
}
// 从哪来回哪去,返回到上一个视图控制器
[self.navigationController popViewControllerAnimated:true];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// SecondViewController.m
// NavigationController
//
#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController (){
ViewController *vc;
}
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Second2";
self.view.backgroundColor = [UIColor greenColor];
UIButton *bt1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 60)];
[bt1 setTitle:@"Pop2->0" forState:UIControlStateNormal];
[bt1 addTarget:self action:@selector(buttonAction1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt1];
}
- (void)buttonAction1 {
if (vc == nil) {
vc = [[ViewController alloc]init];
}
// 返回到最开始的
// [self.navigationController popToRootViewControllerAnimated:true];
//遍历 返回一个数组,是堆栈系统中所有已经进栈的视图控制器
NSArray *array_vc = self.navigationController.viewControllers;
for (UIViewController *item in array_vc) {
if (item.view.tag == 10000) {
//pop到指定视图控制器
[self.navigationController popToViewController:item animated:true];
}
}
//topViewController
UIViewController *topVC = self.navigationController.topViewController;
topVC.view.backgroundColor = [UIColor blueColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4. UIBarButton的基本使用
4.1 4种初始化
/UIBarButtonItem
//初始化1
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonAction2)];
//初始化2 针对于title
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction2)];
//初始化3 自定义view
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
view.backgroundColor = [UIColor orangeColor];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
//初始化4 图片
UIImage *image = [UIImage imageNamed:@"check.png"];
//渲染图片 UIImage -> UIBarButtonItem
UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithImage:newImage style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction1)];
// self.navigationItem.leftBarButtonItem = barButtonItem;
self.navigationItem.rightBarButtonItem = barButtonItem;
4.2 工具栏中使用UIBarButtonItem
//工具栏中使用UIBarButtonItem
[self.navigationController setToolbarHidden:false];
[self.navigationController setNavigationBarHidden:true];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(buttonAction1)];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(buttonAction1)];
UIImage *image = [UIImage imageNamed:@"check.png"];
//渲染图片 UIImage -> UIBarButtonItem
UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc]initWithImage:newImage style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction1)];
//间隔
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
barButtonItem4.width = 50;
//右侧对齐
UIBarButtonItem *barButtonItem5 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *items = [[NSArray alloc]initWithObjects:barButtonItem1,barButtonItem4,barButtonItem2,barButtonItem5,barButtonItem3, nil];
[self setToolbarItems: items];
4.3 设置三种颜色
//导航栏的背景颜色
// self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
// 这种方法设置的 半透明 有颜色梯度:是和白色渐变的
// self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
// 纯蓝色
//UIBarButtonItem的字体颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//中间title的颜色
NSDictionary *colorDic = [[NSDictionary alloc]initWithObjectsAndKeys:[UIColor greenColor],NSForegroundColorAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = colorDic;
5 导航栏视图控制器拖拽快速实现
//push:切换的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"hello");
//segue.destinationViewController 可以拿到目标控制器
UIViewController *vc = segue.destinationViewController;
vc.view.backgroundColor = [UIColor orangeColor];
}
6 模态视图
是视图控制器切换的一种方法
多用于临时弹出的界面:如登陆 帮助
模态视图从下到上出现
//
// ViewController.m
// NavigationController3
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController () {
FirstViewController *firstVC;
}
- (IBAction)buttonAction:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)buttonAction:(id)sender {
if (firstVC == nil) {
firstVC = [[FirstViewController alloc]init];
}
// 弹出视图
[self presentViewController:firstVC animated:true completion:nil];
}
@end
//
// FirstViewController.m
// NavigationController3
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 60 )];
[bt setTitle:@"dismiss" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt];
}
- (void)buttonAction {
//模态视图消失
[self dismissViewControllerAnimated:true completion:^{
NSLog(@"当前模态视图已消失");
}];
}
@end
二. UITabBar的认识
底部TabBar的大小和距离相等
每一个TabBar默认情况下对应一个视图控制器
UITabBar默认高度49
//
// AppDelegate.m
// TabBarController
c
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//1 实例化
UITabBarController *tabBar = [[UITabBarController alloc]init];
self.window.rootViewController = tabBar;
//2 vc
// 属性2 badgeValue 右上角有一个红色提醒
FirstViewController *vc1 = [[FirstViewController alloc]init];
vc1.tabBarItem.title = @"1";
vc1.tabBarItem.badgeValue = @"12";//右上角有一个红色提醒
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.view.backgroundColor = [UIColor greenColor];
vc2.tabBarItem.title = @"2";
// 属性1 title image
UIViewController *vc3 = [[UIViewController alloc]init];
vc3.view.backgroundColor = [UIColor redColor];
vc3.tabBarItem.title = @"设置";
vc3.tabBarItem.badgeValue = @"过期";
//如果给tabBar添加图片,图片会在文字上方,选中后默认变成蓝色
vc3.tabBarItem.image = [UIImage imageNamed:@"setting.png"];
//
// UIViewController *vc4 = [[UIViewController alloc]init];
// vc4.view.backgroundColor = [UIColor redColor];
// vc4.tabBarItem.title = @"4";
//
// UIViewController *vc5 = [[UIViewController alloc]init];
// vc5.view.backgroundColor = [UIColor redColor];
// vc5.tabBarItem.title = @"5";
//
// UIViewController *vc6 = [[UIViewController alloc]init];
// vc6.view.backgroundColor = [UIColor redColor];
// vc6.tabBarItem.title = @"6";
//
// UIViewController *vc7 = [[UIViewController alloc]init];
// vc7.view.backgroundColor = [UIColor redColor];
// vc7.tabBarItem.title = @"7";
//视图控制器非常多的时候,第五个会变为more按钮,点开之后会以列表的形式展示其他的tabBarItem
//3 把vc绑定在TabBar上
tabBar.viewControllers = @[vc1,vc2,vc3];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end