UINavigationController使用

UINavigationController返回总结:

  • 弹出当前视图控制器(弹出并向左显示前一个视图)

 

          [self.navigationController popToViewController:viewController animated:YES];

 

  • 弹出到指定视图控制器(回到指定视图控制器,也就是不只弹出一个)
         [self.navigationController popToRootViewControllerAnimated:YES];

 

  • 弹出到根视图控制器(比如说你有一个“Home”键,也许就会实施这个方法了)
      setNavigationBarHidden:BOOL animated:BOOL

具体应用如下:

  • 假设我的一个navigationController里共有4个viewcontroller,要是在每层ViewController返回上一层ViewController,有一个很简单方法:
       [self.navigationController popViewControllerAnimated:YES];
  • 当然,要想直接返回到根viewcontroller也有现成的方法:
       [self.navigationController popToRootViewControllerAnimated:YES];
  • 不过想要从第5层直接返回到第2层或第3层,则没有现成的方法可以调用了。但这时若能够知道pop回去的ViewController的指针,也就好办了。
       具体写法如下:

       [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] animated:YES];

       在使用时,根据自己返回层的需要,只要改变一下“-2”这个数字就可以达到目的了。

设置背景图片

    UIImage *bgImage = [UIImage imageNamed:@"pic500.jpg"];
   
    UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, 44));
    [bgImage drawInRect:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    UIImage *newBg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navigationController.navigationBar setBackgroundImage:newBg
                                                      forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        self.navigationController.navigationBar.layer.contents=(id)[UIImage imageNamed:@"NavBar-iPhone.png"].CGImage;
    }
   
    [self.navigationController.navigationBar.layer setMasksToBounds:YES];
 

posted @ 2012-11-20 16:43  李伯波  阅读(270)  评论(0编辑  收藏  举报