iOS 第9课 导航视图 NavigationView

来源:互联网 发布:开淘宝店 编辑:程序博客网 时间:2024/06/05 19:46

导航视图和android 中的viewpager类似

//导航栏相关的是navigationItem,控制器相关的是navigationController




0: 首先还是通过纯的代码来实现

0:删除3个文件ViewController.hViewController.mMain.storyboard

1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空

1: 修改appdeleget

////  AppDelegate.m//  NineNavigationDemo////  Created by 千雅爸爸 on 16/10/15.//  Copyright © 2016年 kodulf. All rights reserved.//#import "AppDelegate.h"#import "FirstViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    [self.window makeKeyAndVisible];           FirstViewController *firstViewController = [[FirstViewController alloc]init];    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:firstViewController];    [self.window setRootViewController:navigationController];        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 throttle down OpenGL ES frame rates. 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 inactive 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



2: 创建三个控制器,

2.1: 第一个控制器:

////  FirstViewController.m//  NineNavigationDemo////  Created by 千雅爸爸 on 16/10/15.//  Copyright © 2016年 kodulf. All rights reserved.//#import "FirstViewController.h"#import "SecondViewController.h"@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad {    //导航栏的坐标是(0,20,宽度,44);        //导航栏相关的是navigationItem,控制器相关的是navigationController    [super viewDidLoad];    [self setTitle:@"第一页"];    [self.view setBackgroundColor:[UIColor greenColor]];//    UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];    //设置导航的item    self.navigationItem.rightBarButtonItem = item;        //自定义view作为导航的菜单    UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];    [itemButton setTitle:@"UIButton" forState:UIControlStateNormal];    [itemButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];    [itemButton setFrame:CGRectMake(0,0,100,100)];        UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithCustomView:itemButton];        //设置多个button    self.navigationItem.leftBarButtonItems = @[item,item2];        //设置导航蓝上面的颜色    self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background"] forBarMetrics:UIBarMetricsCompact];//后面的是图片的渲染的样式的。    //UIBarMetricsDefault默认的,背景的图片和颜色是不能重复的,像这里图片会覆盖颜色的    //UIBarMetricsCompac 会使用图片的颜色            //注意了,这里的尺寸里面的位置,    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    [button setTitle:@"Push" forState:UIControlStateNormal];    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];    [button setBackgroundColor:[UIColor blackColor]];    [button setFrame:CGRectMake(100,80,100,100)];    [button addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];//当在内部点击的时候触发    [self.view addSubview:button];        //导航栏控制器在控制多个视图控制的时候的跳转        // Do any additional setup after loading the view.}//相当于入战的操作-(void) pushController{    NSLog(@"点击了first");    [self.navigationController pushViewController:[[SecondViewController alloc]init] animated:YES];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#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

2.2 第二个控制器

////  SecondViewController.m//  NineNavigationDemo////  Created by 千雅爸爸 on 16/10/15.//  Copyright © 2016年 kodulf. All rights reserved.//#import "SecondViewController.h"#import "FirstViewController.h"#import "ThirdViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {    //导航栏相关的是navigationItem,控制器相关的是navigationController        [super viewDidLoad];    //默认的切换的时候是有一些卡顿的。    [self.view setBackgroundColor:[UIColor yellowColor]];    [self setTitle:@"第二页"];    //多个子控制器对象是公用一个导航条的        //添加统一的返回按钮,简单的就是一个子    UIBarButtonItem *leftItem =[[UIBarButtonItem alloc] initWithTitle:@"回退" style:UIBarButtonItemStylePlain target:self action:@selector(popController)];    self.navigationItem.leftBarButtonItem = leftItem;            UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(pushController)];        // Do any additional setup after loading the view.        self.navigationItem.rightBarButtonItem = rightItem;    //公用一套导航条的    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];}-(void )popController{    [self.navigationController popViewControllerAnimated:YES];}-(void)pushController{    [self.navigationController pushViewController:[[ThirdViewController alloc]init] animated:YES];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#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

2.3: 第三个控制器

////  ThirdViewController.m//  NineNavigationDemo////  Created by 千雅爸爸 on 16/10/15.//  Copyright © 2016年 kodulf. All rights reserved.//#import "ThirdViewController.h"@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad {    //导航栏相关的是navigationItem,控制器相关的是navigationController        [super viewDidLoad];    // Do any additional setup after loading the view.    //pop跳转的时候,可以跳转到上一个,也可以跳转到根,也可跳转到指定的    [self setTitle:@"第三个"];    [self.view setBackgroundColor:[UIColor whiteColor]];        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回上一层或者最上层" style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];    //导航栏相关的是navigationItem,控制器相关的是navigationController        self.navigationItem.leftBarButtonItem = leftItem;    }-(void)popViewController{    //可以跳转到第一页,也可以跳转到上一页    //[self.navigationController popViewControllerAnimated:YES];    [self.navigationController popToRootViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#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






0 0
原创粉丝点击