UIKit框架中有各种Bar,UITabBar、UINavigationBar、UIToolbar。Bar对应的就有一些Item,tabBarItem、navigationItem、toolbarItems,再加上UIViewController、UINavigationController、UITabBarController很容易搞糊涂。我看了好久,没看明白。动手敲了下才有一点感觉。

一、联系

一个UINavigationController对应着一个UINavigationBar、UIToolbar,UIToolbar默认不显示,toolbarHidden=NO时显示。一个UITabBarController对应着一个UITabBar.一个UIViewController对应着一个tabBarItem、navigationItem和多个toolbarItems。navigationItem中可以设置左右按钮和中间视图等。

二、代码demo

1.首先代码结构 viewController1-viewController5用于生成5个tabBarItem。viewController、viewController6主要模拟登录注册,有时候需要先让用户登录注册之后才能进入。

2.代码

1.在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    tabBarViewController *tabVc=[[tabBarViewController alloc]init];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor=[UIColor whiteColor];
    self.window.rootViewController=tabVc;
    return YES;
}

2.ViewController.m

//
//  ViewController.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"
#import "ViewController6.h"

@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@ %@",NSStringFromCGRect(self.tabBarController.tabBar.frame),NSStringFromCGRect(self.navigationController.toolbar.frame));
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame=CGRectMake(100, 100, 100, 100);
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:nil];
    self.toolbarItems=@[item1];
}
-(void)btnClick:(id)sender
{
    ViewController6 *vc6=[[ViewController6 alloc]init];
    vc6.title=@"第6页";
    vc6.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc6 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

3.ViewController1.m

//
//  ViewController1.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController1.h"
#import "ViewController2.h"

@interface ViewController1 ()

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

4.ViewController2.m

//
//  ViewController2.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController2.h"
#import "ViewController.h"
#import "navigationViewController.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController *vc=[[ViewController alloc]init];
//    navigationViewController *nav=[[navigationViewController alloc]initWithRootViewController:vc];
//    self.navigationController.toolbarHidden=NO;
//    nav.toolbarHidden=NO;
    [self.navigationController addChildViewController:vc];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

5.ViewController6.m

//
//  ViewController6.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController6.h"

@interface ViewController6 ()

@end

@implementation ViewController6

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@ %@",NSStringFromCGRect(self.tabBarController.tabBar.frame),NSStringFromCGRect(self.navigationController.toolbar.frame));
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(btnClick:)];
    self.toolbarItems=@[item1];
}
-(void)btnClick:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

6.tabBarViewController.m继承UITabBarController

//
//  tabBarViewController.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "tabBarViewController.h"
#import "ViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "navigationViewController.h"

@interface tabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation tabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self steup];
    self.delegate=self;
    self.selectedIndex=2;
    self.moreNavigationController.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1001];
    self.moreNavigationController.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:nil];
    self.moreNavigationController.visibleViewController.navigationItem.title=@"更多功能";
    self.moreNavigationController.visibleViewController.navigationItem.rightBarButtonItem.title=@"编辑";

}
-(void)rightClick:(id)sender
{

}
-(void)steup
{
    ViewController *vc=[[ViewController alloc]init];
    vc.title=@"第0页";
    navigationViewController *nvc=[[navigationViewController alloc]initWithRootViewController:vc];
    nvc.toolbarHidden=NO;
    UITabBarItem *tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第0页" image:[UIImage imageNamed:@"tabbar_homepage_normal"] selectedImage:[UIImage imageNamed:@"tabbar_homepage_selected"]];
    tabBarItem.badgeValue=@"2";
    nvc.tabBarItem=tabBarItem;

    ViewController1 *vc1=[[ViewController1 alloc]init];
    vc1.title=@"第1页";
    navigationViewController *nvc1=[[navigationViewController alloc]initWithRootViewController:vc1];
    nvc1.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第1页" image:[UIImage imageNamed:@"tabbar_convenience_normal"] selectedImage:[UIImage imageNamed:@"tabbar_convenience_selected"]];

    ViewController2 *vc2=[[ViewController2 alloc]init];
    vc2.title=@"第2页";

    navigationViewController *nvc2=[[navigationViewController alloc]initWithRootViewController:vc2];
    nvc2.toolbarHidden=NO;
    nvc2.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第2页" image:[UIImage imageNamed:@"tabbar_electrice_commerce_normal"] selectedImage:[UIImage imageNamed:@"tabbar_electrice_commerce_selected"]];

    ViewController3 *vc3=[[ViewController3 alloc]init];
    vc3.title=@"第3页";
    navigationViewController *nvc3=[[navigationViewController alloc]initWithRootViewController:vc3];
    vc3.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第3页" image:[UIImage imageNamed:@"tabbar_me_normal"] selectedImage:[UIImage imageNamed:@"tabbar_me_selected"]];

    ViewController4 *vc4=[[ViewController4 alloc]init];
    vc4.title=@"第4页";
    navigationViewController *nvc4=[[navigationViewController alloc]initWithRootViewController:vc4];
     nvc4.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第4页" image:[UIImage imageNamed:@"tabbar_convenience_normal"] selectedImage:[UIImage imageNamed:@"tabbar_convenience_selected"]];

    ViewController5 *vc5=[[ViewController5 alloc]init];
    vc5.title=@"第5页";
    navigationViewController *nvc5=[[navigationViewController alloc]initWithRootViewController:vc5];
     nvc5.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第5页" image:[UIImage imageNamed:@"tabbar_homepage_normal"] selectedImage:[UIImage imageNamed:@"tabbar_homepage_selected"]];
    // 默认的顺序
     NSArray *defaultarr=@[nvc,nvc1,nvc2,nvc3,nvc4,nvc5];
    // 自定义的顺序
     NSMutableArray *newarr=[[NSMutableArray alloc]init];
    //获取保存的title数组
    NSArray *titles=[[NSUserDefaults standardUserDefaults] arrayForKey:@"vcs"];
    // 第一次启动为nil
    if (titles==nil) {
        newarr=[defaultarr copy];
    }
    //根据自定义title数组 设置newarr数组
    for (NSString *s in titles) {
        for (navigationViewController *nvc in defaultarr) {
            if ([s isEqualToString:nvc.visibleViewController.title]) {
                [newarr addObject:nvc];
            }
        }
    }
    self.viewControllers=newarr;

}
//UITabBarControllerDelegate
//是否可以选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    return YES;
}
//选中某个viewController
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//    NSLog(@"%@",viewController.title);
}
//开始自定义viewControllers
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
    NSLog(@"%@",viewControllers);
}
//即将编辑结束
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    NSLog(@"%d",changed);
}
//编辑结束
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    //记下选择的选项顺序,方便下次启动时显示
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    if (!changed) {
        return;
    }
    for (UIViewController *vc in viewControllers) {
        [arr addObject:vc.title];
    }
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:arr forKey:@"vcs"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

7.navigationViewController.m

//
//  navigationViewController.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "navigationViewController.h"

@interface navigationViewController ()<UINavigationControllerDelegate>

@end

@implementation navigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
//    {
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    }
    self.navigationController.navigationBar.translucent=NO;
    self.delegate=self;

}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"%@",viewController.title);
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

实现效果:

UIKit 框架之Bar、Controller的更多相关文章

  1. Tab Bar Controller和Navigation Controller混合使用详细教程

    在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...

  2. [New learn] UIKit 框架类

    NSObject NSObject is the root class of most Objective-C class hierarchies. NSDataAsset The NSDataAss ...

  3. UIKit框架使用总结--看看你掌握了多少

    一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...

  4. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  5. iOS第八课——Navigation Controller和Tab bar Controller

    今天我们要学习Navigation Controller和Tab bar Controller. Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器. ...

  6. UIKit框架

    在今后的应用程序构建中,会陆续使用各式各样的控件,因此UIKit框架的引入是必不可少的! 一.简介 UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口.应 ...

  7. iOS开发中的错误整理,Changing the delegate of a tab bar managed by a tab bar controller is not allowed

    iOS [错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'] 错误:'Ch ...

  8. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  9. 基础框架Fundation和UIkit框架的定义和使用

    Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...

随机推荐

  1. MYC编译器源码之词法分析

    前文  .NET框架源码解读之MYC编译器 和 MYC编译器源码分析之程序入口 分别讲解了 SSCLI 里示例编译器的架构和程序入口,本文接着分析它的词法分析部分的代码. 词法解析的工作都由Tok类处 ...

  2. js插件开发的一些感想和心得

    起因 如果大家平时做过一些前端开发方面的工作,一定会有这样的体会:页面需要某种效果或者插件的时候,我们一般会有两种选择:1.上网查找相关的JS插件,学习其用法2.自己造轮子,开发插件. 寻找存在的插件 ...

  3. 项目笔记---事半功倍之StyleCop(一)

    前言 曾几何时,你是否在看别人代码的时候总是在抱怨代码没有注释,命名不规范,代码风格不统一,代码可读性差?是否有一个适合团队开发规范的检查工具? 答案就是大名鼎鼎的StyleCop代码检查插件,有了这 ...

  4. python 实现过滤出tomcat日志中含有ERROR 或Exception 的行并保存在另一个文件

    遍历多个tomcat日志文件,找出含有ERROR 和Exception 的日志,并把该行日志输出到另一个文件中:(这里为了体现python模块导入的知识,所有建立了多个文件夹和模块) 项目结构: co ...

  5. ASP.NET MVC 学习笔记(三),排序加查找

    首先先说排序 非常的简单 代码如下 //创建视图 public ViewResult Index() { //升序排列 IEnumerable<Product> Prodcuts = re ...

  6. NET Core2.1 WEB老项目迁移

    .NET Core2.1 版本新增功能不在赘述. NET Core2.1更新链接 如果开发需要安装Net Core2.1SDK,及Runtime. .NET Core2.1安装地址. 接下来是WEB ...

  7. MVVM Light 新手入门(3) :ViewModel / Model 中定义“事件” ,并在View中调用 (无参数调用)

    今天学习MVVM架构中“事件”的添加并调用,特记录如下,学习资料均来自于网络,特别感谢翁智华 的 利刃 MVVMLight 6:命令基础 在MVVM Light框架中,事件是WPF应用程序中UI与后台 ...

  8. c#开发sqlite

    教程:https://www.cnblogs.com/icebutterfly/p/7850689.html 下载sqlite-netFx40-setup-bundle-x64-2010-1.0.96 ...

  9. [5.19 线下活动]Docker Meetup杭州站—拥抱Kubernetes,容器深度实践

    对本次线下活动感兴趣的朋友,欢迎点击此处报名,领取免费票. 今年3月,Docker刚刚过完5岁生日,五年期间,Docker也逐渐在技术和实践方面趋于成熟,更是在去年年底主动拥抱Kubernetes. ...

  10. C#穿透session隔离———Windows服务启动UI交互程序

    在Windows服务里面启动其他具有界面的应用程序,需要穿透session隔离,尝试了很多种方法,都可行,现在一一列举下来,并写下几个需要注意的地方. 需要注意的地方 首先要将服务的Account属性 ...