欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UINavigationController使用

程序员文章站 2022-07-15 19:10:05
...

1,UINavigationController简介

          导航控制器,ios常用

          使用堆栈存储。根视图控制器在堆栈最底层。

 

[self.navigationController pushViewController:viewController animated:YES];   //入栈 进入viewController视图控制器
[self.navigationController popViewControllerAnimated:YES];  //出栈,返回上一个视图控制器
[self.navigationController popToViewController:UIViewController animated:YES];  //返回到指定的试图控制器
[self.navigationController popToRootViewControllerAnimated:YES];   //返回根控制器    

 

 

2,UINavigationController的结构组成

 

          UINavigationController由Navigation bar  ,Navigation View ,Navigation toobar等组成;     

 

3,UINavigationController的创建

             UINavigationController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor groupTableViewBackgroundColor];  
    ViewController *rootVC = [[ViewController alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController: rootVC];
    [self.window setRootViewController: navigation];
    [self.window makeKeyAndVisible];
    return YES;
}

 

4,UINavigationBar

               (1)默认NavigationBar

                  navigationBar--导航条,iOS7以后默认是透明的,iOS7以前默认是不透明的。

                  navigationBar在透明情况下,与contentView会重合一部分区域

                  navigationBar在不透明情况,ContentView跟在navigationBar下面

                  navigationBar竖屏下默认高度44,横屏下默认高度32

               (2)自定义NavigationBar

[self.navigationController.navigationBar setTranslucent: NO];    //显示出NavigationBar
self.navigationController.navigationBar.backgroundColor = [UIColor redColor]; // 设置导航条的颜色
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tupianName"] forBarMetrics:UIBarMetricsCompact];   //导航条加背景图片

               (3)自定义左侧按钮

- (void)viewDidLoad {
    [super viewDidLoad];
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    leftBtn.frame = CGRectMake(0, 10, 93, 30);
    leftBtn.backgroundColor = [UIColor orangeColor];
    [leftBtn addTarget:self action:@selector(onTap) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
}

                (4)自定义右侧按钮    

                         与左侧按钮类似,区别在于:

self.navigationItem.rightBarButtonItem = rightItem;

                (5)自定义中间视图

UIView * centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 120, 30)];
centerView.backgroundColor = [UIColor greenColor];
self.navigationItem.titleView = centerView;

      

5,UINavigationToolbar

               (1)显示Toolbar

[self.navigationController setToolbarHidden:NO animated:YES] ;

               (2)ToolBar上添加UIBarButtonItem

   UIBarButtonItem *camera=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(ClickToolBarButton)];  
   [camera setWidth:80];  
   UIBarButtonItem *refresh=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(ClickToolBarButton)];  
   [refresh setWidth:80];  
   UIBarButtonItem *reply=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(ClickToolBarButton)];  
   [reply setWidth:80];  
   UIBarButtonItem *compose=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(ClickToolBarButton)];  
   [compose setWidth:80];  
     
   UIBarButtonItem *splitspace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];  
     
   [self setToolbarItems:[NSArray arrayWithObjects:splitspace,camera,splitspace,refresh,splitspace,reply,splitspace,compose,splitspace, nil nil]];  
    

 

相关标签: ios Objective-c