Fork me on GitHub

UI- UINavigationController UITabBarController 使用总结

#pragma mark - UINavigationController UITabBarController  ======================================

 控制器的切换方式 --> Push / Model  UINavigationController 第5点 和 UITabBarController 第4点 有使用说明

 一、UINavigationController

 1. 一个iOS的app很少只由一个控制器组成,除非这个app极其简单,当app中有多个控制器的时候,我们就需要对这些控制器进行管理,导航控制器仅仅是一个容器,可以在导航控制器中压入或弹出多个视图控制器,导航控制器是以栈结构的形式管理这些视图控制器

 2. 为了便于管理控制器,iOS提供了2个比较特殊的控制器 --> UINavigationController / UITabBarController

 3. UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等组成。

 4. UINavigationController的使用步骤

    4.1 初始化UINavigationController

    4.2 设置UIWindow的rootViewController为UINavigationController

    4.3 根据具体情况,通过push方法添加对应个数的子控制器

 5. UINavigationController的子控制器

    5.1 UINavigationController以栈的形式保存子控制器

    @property(nonatomic,copy) NSArray *viewControllers;

    @property(nonatomic,readonly) NSArray *childViewControllers;

 

    5.2 使用push方法能将某个控制器压入栈   使用push 必须在导航控制器中使用

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

 

    5.3 使用pop方法可以移除控制器

    5.31 将栈顶的控制器移除

    - (UIViewController *)popViewControllerAnimated:(BOOL)animated;

    5.32 回到指定的子控制器

    - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

    5.33 回到根控制器(栈底控制器)

    - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

 

    5.4 关于pushViewController:animated:的动画效果一般情况我们使用默认的动画就行,但我们也可以自定动画内容,我们可以使用CATransition来实现转换动画效果,代码如下:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

     

     if (indexPath.row == 0) {

         OneViewController *oneViewController = [[OneViewController alloc] init];

         

         CATransition *animation = [CATransition animation];

         [animation setDuration:0.5];

         [animation setType: kCATransitionMoveIn];

         [animation setSubtype: kCATransitionFromTop];

         [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

         

         [self.navigationController pushViewController:oneViewController animated:NO];

         oneViewController = nil;

         

         [self.navigationController.view.layer addAnimation:animation forKey:nil];

 

     }

     

    }

 

 6. 修改导航条的内容

    导航栏的内容由栈顶控制器的navigationItem属性决定

    UINavigationItem有以下属性影响着导航栏的内容

    6.1 左上角的返回按钮

    @property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;

    6.2 中间的标题视图

    @property(nonatomic,retain) UIView          *titleView;

    6.3 中间的标题文字

    @property(nonatomic,copy)   NSString        *title;

    6.4 左上角的视图

    @property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;

    6.5 UIBarButtonItem *rightBarButtonItem  右上角的视图

    @property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

 

 7. 设置导航栏是否显示

    setNavigationBarHidden:BOOL animated:BOOL

 8. Toolbar在UINavigationController默认是隐藏的,可通过下面的代码显示出来。

    self.navigationController.toolbarHidden = NO; 或

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

 9. 设置导航条的一些属性

    9.1 定义一个导航控制器,初始化导航控制器的根视图

    ViewController *rootVC = [[ViewController alloc]init];

    UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:rootVC];

    9.2 导航条的样式

    navVC.navigationBar.barStyle = UIBarStyleDefault;

    9.3 导航条默认为透明的,坐标原点在屏幕的左上角; 如果把导航条设置为不透明,坐标原点在导航条下方的左上角

    navVC.navigationBar.translucent = YES;

    navVC.navigationBar.backgroundColor = [UIColor yellowColor];

    9.4 设置导航条左右两侧按钮文字的颜色

    navVC.navigationBar.tintColor = [UIColor orangeColor];

    9.5 设置导航条的颜色

    navVC.navigationBar.barTintColor = [UIColor greenColor];

    9.6 在设置导航条背景图片时,需要注意的是,图片高度应该包括状态栏的高度20,如果不包括,状态栏为黑色

    要求导航条的背景图片高度应该为64

    [navVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"head"] forBarMetrics:UIBarMetricsDefault];

    9.7 默认情况下,导航控制器的标题就是视图控制器的标题

    9.71 一般是单独设置导航控制器的每个子控制器的navigationItem的属性

    设置导航选项的相关属性:

    9.72 标题

    rootVC.title = @"XXXX";

    默认情况下,返回按钮旁边是上一个视图控制器设置的导航标题

    9.73 重新设置返回按钮文字 若导航控制器的跟控制器为第一个视图控制器, 不需要设置返回

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"回来B" style:UIBarButtonItemStylePlain target:self action:nil];

    rootVC.navigationItem.backBarButtonItem = backItem;

 

    9.74自定义右侧导航按钮

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"navVCY" style:UIBarButtonItemStylePlain target:self action:nil];

    rootVC.navigationItem.rightBarButtonItem = rightItem;

 

    9.75自定义左侧导航按钮

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil];

    rootVC.navigationItem.leftBarButtonItem = leftItem;

 

 10.隐藏导航条

    self.navigationController.navigationBarHidden = YES;

 

 二、UITabBarController=======================================================================

 1. UITabBarController的简单使用

    初始化UITabBarController

    设置UIWindow的rootViewController为UITabBarController

    根据具体情况,通过addChildViewController方法添加对应个数的子控制器

 2. UITabBarController的子控制器

    UITabBarController添加控制器的方式有2种

    2.1 添加单个子控制器

    - (void)addChildViewController:(UIViewController *)childController;

    2.2 设置子控制器数组

    @property(nonatomic,copy) NSArray *viewControllers;

 3. UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定

    UITabBarItem有以下属性影响着UITabBarButton的内容

    3.1 标题文字

    @property(nonatomic,copy) NSString *title;

    3.2 图标

    @property(nonatomic,retain) UIImage *image;

    3.3 选中时的图标

    @property(nonatomic,retain) UIImage *selectedImage;

    3.4 提醒数字

    @property(nonatomic,copy) NSString *badgeValue;

 4. Model

    4.1 以Modal的形式展示控制器

    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion

 

   - 4.2 使用show的形式 

   - (void)showViewController:(UIViewController *)vc sender:(nullable id)sender

 

    4.3 关闭当初Modal出来的控制器

    - (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

 

 5. 例: 

    5.1 先创建控制器对象

    FirstVC *firstVC = [[FirstVC alloc]init];

    SecondVC *secondVC = [[SecondVC alloc] init];

    ThirdVC *thirdVC = [[ThirdVC alloc]init];

    FourthVC *fourthVC = [[FourthVC alloc]init];

    FifthVC *fifthVC = [[FifthVC alloc]init];

    SixthVC *sixthVC = [[SixthVC  alloc]init];

    5.2 设置控制器对象标题

    firstVC.title = @"first";

    secondVC.title = @"second";

    thirdVC.title = @"third";

    fourthVC.title = @"fourth";

    fifthVC.title = @"fifth";

    sixthVC.title = @"sixth";

    5.3 把FirstVC用导航包装 (其他控制器也都可以包装)

    UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:firstVC]

    5.4 创建TabBar控制器对象

    UITabBarController *tabBarVC = [[UITabBarController alloc]init];

 

    5.5 设置Tabbar的子控制器对象, 需要注意的是,TabBar控制器最多只能显示五个子视图,如果超过5个,它会只显示4个,把其他的放More选项中

    tabBarVC.viewControllers = @[navVC, secondVC, thirdVC, fourthVC,fifthVC, sixthVC];

    5.6 设置tabBar的条的颜色

    tabBarVC.tabBar.barTintColor =[ UIColor orangeColor];

    tabBarVC.tabBar.tintColor = [UIColor greenColor];

    tabBarVC.tabBar.backgroundImage = [UIImage imageNamed:@"tabbg"];

    5.7 设置tabBarItem的相关属性

    使用原始图片进行渲染

    UIImage *firstImg =[[UIImage imageNamed:@"tab_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    firstVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"第一页" image:firstImg  tag:1];

    secondVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Second" image:[UIImage imageNamed:@"tab_1"] selectedImage:[UIImage imageNamed:@"tab_2"]];

    secondVC.tabBarItem.badgeValue = @"10"; //提醒数字

posted @ 2016-04-16 13:10  极度恐慌_JG  阅读(290)  评论(0编辑  收藏  举报