iOS UINavigationController + UITabBarController

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

由于项目要使用到选项卡,所以使用了UINavigationController 嵌套 UITabBarController搭建一个简单的模型,欢迎各位一起讨论其中的不足,一起学习,一起进步。


首先,写了一个ViewController继承     UITabBarController   易修改和复用,名字为 BaseTabBarController 主要有两个方法,用于设置UITabBarController里包含的每个viewcontroller和背景。

/**
*  设置选项卡ViewController
*
*  @param viewController   viewController
*  @param selectedImage    选中的图片
*  @param customImage      默认图片
*  @param title            标题
*  @param highLigthedColor 标题选中颜色
*  @param customColor      标题默认颜色
*  @param tag              tag
*  @return                 UINavigationController
*/


-(UINavigationController *)setTabViewController:(UIViewController *)viewController
              selectedImage:(UIImage *) selectedImage
                customImage:(UIImage *)customImage
                     title :(NSString *)title
          highLigthedColor :(UIColor *)highLigthedColor
               customColor :(UIColor *)customColor
                        tag:(NSInteger)tag

{

  UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
   
  navigationController.navigationBar.hidden = YES;
   
      UITabBarItem * icon = [[UITabBarItem alloc] init];
   
       icon.tag = tag;
   
      [icon setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:customImage];
  
   
   
    if(title != nil){
        [icon setTitle:title];
        [icon setTitleTextAttributes:@{UITextAttributeTextColor :customColor} forState:UIControlStateNormal];
        [icon setTitleTextAttributes:@{UITextAttributeTextColor :highLigthedColor} forState:UIControlStateHighlighted];


    }
    
    navigationController.tabBarItem = icon;
    
    return navigationController;

}


/**
 *  设置选项卡的背景颜色
 *
 *  @param colorImageView 颜色图片
 */


-(void)setBackGroundColor:(UIImageView *)colorImageView

{

 [[UITabBar appearance] setBackgroundImage:IMG(@"color_2")];

}


然后在你需要用到选项卡的ViewController 中 继承BaseTabBarController 

然后添加对应的VIewcontroler


 HoemPageViewController * homePageViewController = [[HoemPageViewController alloc] init];
 
 UINavigationController * nav_homePageViewController = [self setTabViewController:homePageViewController
                                                                     selectedImage:IMG(@"tabbar_home_selected_os7")
                                                                       customImage:IMG(@"tabbar_home_os7")
                                                                             title:@"首页"
                                                                  highLigthedColor:UIColorFromRGB(0x067AB5)
                                                                       customColor:UIColorFromRGB(0x73c34a)
                                                                               tag:1];


 DiscoverViewController * disCoverViewController = [[DiscoverViewController alloc] init];

 UINavigationController * nav_disCoverViewController =  [self setTabViewController:disCoverViewController
                                                                         selectedImage:IMG(@"tabbar_discover_selected_os7")
                                                                           customImage:IMG(@"tabbar_discover_os7")
                                                                                 title:nil
                                                                      highLigthedColor:nil
                                                                           customColor:nil
                                                                                   tag:2];

就暂写两个为例,需要多的再添加,一般都是4或5个。

接下创建一个数组,存上面创建viewcontroller

   NSArray * views = @[nav_homePageViewController,nav_disCoverViewController];

   self.viewControllers = views;
   self.delegate = self;

然后实现代理:

#pragma UITabBarControllerDelegate   delegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //选中的时候
  
    switch (viewController.tabBarItem.tag) {
         //根据tag来判断选中的是哪个选项
            
        default:
            break;
    }
    
//如果您觉得系统自带的小红圈数字不喜欢的话,下面提供一种方式,可以用图片代替系统的小红圈。    

//  自定义红色小数字   
    for(UIView *view in tabBarController.tabBar.subviews) {
        if([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"color_2"]];
    image.frame = CGRectMake(30, 0, 20, 20);
    [tabBarController.tabBar addSubview:image];
    
    
}

下面是关于 UINavigationController 和  UITabBarController 的一些属性设置小知识

设置标题栏的的颜色,ios7的话使用   [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

iOS 6 使用   _navigationController.navigationBar.tintColor = UIColorFromRGB(0x067AB5);

ios6及一下选中某一项的时候都会有一个灰色的背景框,可以使用下面该方法,用一张透明的图片把他给去掉,如果您有更好的方法也希望提供一起学习。

 [[UITabBar appearance] setSelectionIndicatorImage:IMG(@"item_selected_bg")];


好了 ,收工,准备下班。。。






IOS UITabBarControllerUINavigationController实现多级控制器管理 UITabBarController的使用 引入:上一篇博客有提到UINavigationController的使用,这篇博客我先重点介绍这个和它使用一样广泛的UITabBarController,我们比较常见的是微信的下方四个按钮,可以切换不同的页面。每个页面都对应着不同的控制器,在UITabBarController中,管理着若干子控制器,他们之间是并列的关系,在添加到TabBar之后就不会被... 阅读详情

相关推荐

iOS-UITabBarController+UINavigationController的混合使用

绝大多数APP都采用UITabBarController+UINavigationController的设计模式,是一种很主流的经典的设计方式。 一、一个 UITabBarController 中嵌套多个 UINavigationController(最流行的方式) //1.创建三个子控制器 ViewController *vc1 = [[ViewController a...

SunSatan的博客 2009

IOS之UIViewController、UINavigationControllerUITabBarController的整合使用

UINavigationControllerUITabBarControlleriOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下: @interface UITabBarController : UIViewController @interface UINavigationController : UIViewController

LVXIANGAN的专栏 1159

iOS项目开发实战(Swift)—初探UITabbarControllerUINavigationController的集成

1.UITabbarController 分栏控制器,用来进行ViewController页面的切换,这些页面是并列的。最下面一栏是Tabbar,其中的按钮是TabbarItem 2.UINavigationController 导航控制器,也是用来进行ViewController页面的切换,但这些页面不是并列,有层次关系。最上面一栏是Navigationbar,用navigatio

hackerzchao 4296

iOSUINavigationControllerUITabBarController的使用

1、AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]

yuxikuo_1的专栏 935

ios6.0 UINavigationControllerUITabBarController 支持屏幕自动旋转问题

1、描述 self.window.rootViewController = navigationController;

东名夜雨 的专栏 2876

iOS 笔记二:Multiple MVCs 、UINavigationControllerUITabBarController

实现方式,使用: 1. UINavigationController 2. UITabBarController Add new MVC to your storyboard: 1.Drag "View Controller" form Object Palette. 2.Create a subclass of UIViewController using New File m

Romantic_Energy的专栏 725

iOS UINavigationControllerUITabBarController的组合使用

1.导航类型 UINavigationController 适用于父子页面的跳转 UITabBarController 适用于平级页面的跳转   2.presentViewController / dismissViewControllerAnimated和pushViewController / popViewController (1)只有从UINavigatio

ANDY_GUO_wei的专栏 937

iOS -App主流框架UINavigationController && UITabBarController的简单使用

一个iOSapp几乎没有由一个控制器组成,除非这个app非常简单。 当app中有多个控制器的时候,就需要对这些控制器进行管理,用1个控制器去管理其他多个控制器; 如图所示: 苹果公司为我提供了两个特殊的控制器,UINavigationControllerUITabBarController去管理其它控制器; ...

dianyao5745的博客 404

iOSUINavigationController UITabBarController UITableView学习笔记

转载说明: 发现这哥们的写的iOS学习笔记挺不错,图文并茂,转了。 作者:一片枫叶; 博客地址:http://www.cnblogs.com/smileEvday/ (1)UINavigationController使用详解   有一阵子没有写随笔,感觉有点儿手生。一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家。   废话少说回到正

Winters_huang'Blog 1593

iOS Storyboard UI框架搭建,UITabBarController UINavigationController

Storyboard 是苹果在iOS 5 中引入的技术方案,讲原来 多个nib 的复杂逻辑集中到一个集合里面,但是在多人合作开发中Storyboard也存在很多问题,经常要处理冲突,已经合并带来的痛苦。         最近因为要开发新的项目,所以也研究一下Storyboard ,个人认为Storyboard 对于拆分业务逻辑,和多人开发非常方便,而且整个项目的UI架构也非常清晰,不必多说以下是

u012269653的博客 2449

iOS开发——视图切换——UITabBarController——UINavigationController——模态(Model)

视图切换原理: 1.UITabBarController:以平行的方式管理视图,各个视图之间的关系并不大,每个加入到UITabBarController的视图都会进行初始化(只加载当前显示的viewcontroller其他的只做初始化)即使当前不显示在界面上,相对比较占用内存 2.UINavigationController:以栈的方式管理视图,只有栈顶得控制器能够显示,各个视图的切换实际就是

Hyman_YH的专栏 1014

iOS开发——纯代码界面(UITabBarControllerUINavigationController,UIImageView)

一、创建UITabBarControllerUINavigationController(标签栏控制器和导航控制器)创建两个类,FirstViewController和SecondViewController。修改AppDelegate.m中的代码。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti

Xiaowei's Blog 2249

iOS】UIViewController、UINavigationControllerUITabBarController的整合使用

原文http://blog.csdn.net/rongxinhua/article/details/20214293 UINavigationControllerUITabBarControlleriOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下: @interface UITabBa...

weixin_34351321的博客 107

iOS三种视图切换 (UITabBarControllerUINavigationController,模态窗口)

概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单。在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarControllerUINavigationController模态窗口 UITabBarController iOS三种视图切换的原理各不相同: UITabBarController:以平行

Mr_Big_Kong的博客 1292
上一篇: iOS 二维码,条形码扫描
下一篇: CocoaPods的安装和使用介绍
方阿宅
博客等级 码龄15年 0粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值