UINavigationController导航控制器

  UINavigationController创建有两种,第一种就是创建一个类继承UINavigationController,这个很简单,系统都会帮你弄好,你只需要在原有的基础上添加一点东西就好。不需要自己写太多东西,第二种方法需要自己写很多,要把UIViewController转换成UINavigationController。

// 导航试图控制器也是一个视图控制器,TA管理了多个子视图控制器,是系统提供给我们的空气视图控制器
    // 导航试图控制器至少管理一个子视图控制器,这个视图控制器成为导航试图控制器的根视图控制器
    // 如果我们的程序想要采用导航视图控制器进行布局,我们需要制定window的跟视图控制器为导航试图控制器
    
    RootViewController *rootViewController = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    // 设置导航视图控制器的显隐
//    naVC.navigationController.navigationBarHidden = YES;
    
    // 设置导航栏样式
    naVC.navigationBar.barStyle = UIBarStyleDefault;
    
    // 设置导航条的背景颜色
//    naVC.navigationBar.backgroundColor = [UIColor greenColor];
    
    // 设置导航栏颜色
//    naVC.navigationBar.barTintColor = [UIColor greenColor];
    
    // 设置导航栏元素颜色
//    naVC.navigationBar.tintColor = [UIColor blackColor];
    
    self.window.rootViewController = naVC;

  这样我们就成功创建一个UINavigationController并设置好了一些属性。然后我们需要给RootViewController添加一些属性了。

// 如果不给view设置背景色,那么跳转的时候会看起来是卡主了
self.view.backgroundColor = [UIColor whiteColor]; // 每一个加到导航视图控制器内部的试图控制器自带一个属性加navigationItem,可以 配置当前页面导航条的显示内容,比如左右按钮,标题等 self.navigationItem.title = @"标题"; // 1、显示标题 // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)]; // 2、使用系统自带图标样式 // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(leftAction)]; // 3、使用自定义图片显示 UIImage *image = [UIImage imageNamed:@"phone"]; NSLog(@"%@", image); UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)]; self.navigationItem.leftBarButtonItem = left; [left release]; // 4、使用自定义视图显示 // UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; // UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:mySwitch]; // self.navigationItem.leftBarButtonItem = left; // [mySwitch release]; // [left release]; // 设置左、右按钮显示多个 UIBarButtonItem *first = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)]; UIBarButtonItem *second = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(bookmark)]; self.navigationItem.rightBarButtonItems = @[first, second]; [first release]; [second release]; // 设置导航条上的标题视图 UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"iOS", @"学习"]]; seg.selectedSegmentIndex = 0; // seg.momentary = YES; self.navigationItem.titleView = seg; [seg release]; // 设置导航栏的显隐属性 // self.navigationController.navigationBarHidden = NO; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; view.backgroundColor = [UIColor blackColor]; [self.view addSubview:view]; [view release]; // 导航栏半透明效果(iOS7以后 默认为YES) // 当半透明效果开启时 屏幕左上角为坐标原点 // 关闭时 导航栏左下角为坐标原点 self.navigationController.navigationBar.translucent = NO;

  好了,一个简单的导航控制器就完成了。后续会有传值。

posted on 2016-02-22 15:28  小鸟成长记  阅读(206)  评论(0编辑  收藏  举报

导航