UINavigationController

AppDelegate.m 中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    // Override point for customization after application launch.


    RootViewController *root = [[RootViewControlleralloc] init];

    

    //一个特殊的视图控制器,导航控制器通过视图控制器栈(栈容器)来管理多个视图控制器

    //创建导航控制器,并传入一个vc对象,作为根视图控制器:作为栈容器的第一个元素(基栈)

    UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:root];

    self.window.rootViewController = nav;

    

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}


二:UINavigationController的一些小知识

- (void)viewDidLoad

{

    [superviewDidLoad];

   //1:view是透明色 push后会卡顿,解决办法,将view设置任意其他颜色

    self.view.backgroundColor = [UIColorredColor];

  //隐藏导航条,隐藏父类的导航条

    [self.navigationControllersetNavigationBarHidden:YES];


    //导航控制器的导航条(320*44)

    //(iOS7)默认情况下视图控制器的view 0 0点在屏幕左上角.vc.view的子视图y值至少要64

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setFrame:CGRectMake(10,74,300,50)];

    [btn setTitle:@"push"forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnClicked)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn];

    

    /****导航条****/

    //每个导航控制器有且一个导航条,所有vc共用此导航条

    //大小固定(320*44)

    //UINavigaitonBar 视图控件  贴图

    //设置导航条处于人像模式(iPhone竖屏)下的背景图片

    [self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"navBg.png"]forBarMetrics:UIBarMetricsDefault];

    //设置处于风景模式(iPhone横屏)下的背景图片

    //风景模式下导航条的大小(480*32)

   // [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg-32.png"] forBarMetrics:UIBarMetricsLandscapePhone];

}


- (void)btnClicked{

    SubViewController *sub = [[SubViewControlleralloc] init];

    //通过导航控制器,对sub执行入栈操作

    //navigationController 通过UIViewController类别的get方法,拿到导航控制器对象,前提是,vc被添加到导航控制器中 +1

    [self.navigationControllerpushViewController:sub animated:YES];


   pop方法:  

    //直接回到根视图控制器

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

   或者:

    //viewControllers数组,所有添加到栈中vc 都在数组中,数组中vc的下标与添加到导航控制器中的顺序一致

    NSLog(@"vc count:%d",self.navigationController.viewControllers.count);

    //位置0为根视图控制器,每个视图控制器都有一个view,位置1为第二个视图,依次到最后视图。pop到对应的视图控制器。

    [self.navigationControllerpopToViewController:[self.navigationController.viewControllersobjectAtIndex:1]animated:YES];




}

三:UINavigationItem创建方法

- (void)viewDidLoad

{


    [super viewDidLoad];

    

    //每一次进行vc的入栈操作,导航条就会自动创建一个UINavigationItem的对象,并对item对象执行入栈操作

    //通过vc能够拿到对应的item对象,并通过item对象设置导航条的显示

    //设置标题

    self.navigationItem.title = @"root";

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,30)];

    view.backgroundColor = [UIColor orangeColor];

    //设置标题视图,使用最多的是放公司的logo

    self.navigationItem.titleView = view;

   

    

    //UIBarButtonItem 类似于UIButton,只用于导航条和工具栏上

    //创建item对象并设置标题和风格样式

    //UIBarButtonItem导航条按钮,类似于按钮,只用于导航条和工具栏,但不是button.


    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(itemClicked:)];

    //覆盖掉自动创建的backItem对象,显示到导航条的左侧

    self.navigationItem.leftBarButtonItem = leftItem;


    

    //利用系统预置样式,创建item对象

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemClicked:)];

    

    //创建item最重要的一个方法

    UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [customBtn setFrame:CGRectMake(0,0,22,22)];

    [customBtn setBackgroundImage:[UIImage imageNamed:@"itemImage.png"] forState:UIControlStateNormal];

    [customBtn addTarget:self action:@selector(customBtnClicked) forControlEvents:UIControlEventTouchUpInside];

    

    //利用自定义的视图,创建item对象

    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:customBtn];

    NSArray *array = [NSArray arrayWithObjects:item1,item2, nil];


    //设置一组item

    self.navigationItem.rightBarButtonItems = array;

    

}



   

你可能感兴趣的:(UINavigationController)