UI_标签导航控制器

这篇博客介绍了如何在iOS应用中自定义TabBarController,包括添加导航控制器、设置自定义标题和图片、设置底部标签的样式和颜色,并展示了如何为每个标签设置角标值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "AppDelegate.h"

//#import "RootViewController.h"

//#import "OneViewController.h"

//#import "TwoViewController.h"

#import "RootTabBarController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    /*

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

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

    

    

    OneViewController *oneVC = [OneViewController new];

    UINavigationController *naVC1 = [[UINavigationController alloc]initWithRootViewController:oneVC];

    

    TwoViewController *twoVC = [TwoViewController new];

    UINavigationController *naVC2 = [[UINavigationController alloc]initWithRootViewController:twoVC];

  


//tabBarController的使用

        //初始化

    UITabBarController *myTabBarController = [UITabBarController new];

        //将创建好的导航控制器放入tabBarController

        //tabBarController添加子视图控制器

    myTabBarController.viewControllers = @[naVC,naVC1,naVC2];

        //设置底部的item

    naVC.tabBarItem.title = @"";

        //设置图片,去掉图片渲染

    naVC.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        //设置tabBarItem的颜色

    myTabBarController.tabBar.tintColor = [UIColor redColor];

        //设置tabBar的颜色

    myTabBarController.tabBar.barTintColor = [UIColor yellowColor];

        //设置item的角标

    naVC.tabBarItem.badgeValue = @"10";

        //设置默认被选中的item(切换界面就是依靠此属性的改变,默认值为0)

    myTabBarController.selectedIndex = 0;

    //myTabBarController.selectedViewController = naVC1;    等同上句

   

    

    //添加第二个

    naVC1.tabBarItem.title = @"";

    naVC1.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

    

    //添加第三个

    naVC2.tabBarItem.title = @"";

    naVC2.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

    

    

        //tabBarController设置为window的根视图控制器

    self.window.rootViewController = myTabBarController;

  */

 

    RootTabBarController *rootTabBarController = [RootTabBarController new];

    self.window.rootViewController = rootTabBarController;

    

    

    

    

    


    


    

    

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}@end




#import "RootTabBarController.h"

#import "RootViewController.h"

#import "OneViewController.h"

#import "TwoViewController.h"

#import "ThreeViewController.h"

#import "FourViewController.h"

#import "FiveViewController.h"

#import "SixViewController.h"

@interface RootTabBarController ()


@end


@implementation RootTabBarController


- (void)viewDidLoad {

    [super viewDidLoad];

//第一个

//    RootViewController *rootVC = [RootViewController new];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];

//    

//    navC.tabBarItem.title = @"";

//    navC.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//    navC.tabBarItem.badgeValue = @"1";

//第一个

    OneViewController *oneVC = [OneViewController new];

    UINavigationController *one = [[UINavigationController alloc]initWithRootViewController:oneVC];

    

   // one.tabBarItem.title = @"";

   // one.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

        //系统的图标风格

    one.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:1001];

    one.tabBarItem.badgeValue = @"1";

//第二个

    TwoViewController *twoVC = [TwoViewController new];

    UINavigationController *two = [[UINavigationController alloc]initWithRootViewController:twoVC];

    

   // two.tabBarItem.title = @"";

   // two.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    two.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1002];

    two.tabBarItem.badgeValue = @"2";

    

    

//第三个

    ThreeViewController *threeVC = [ThreeViewController new];

    UINavigationController *three = [[UINavigationController alloc]initWithRootViewController:threeVC];

    

    //three.tabBarItem.title = @"";

    //three.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    three.tabBarItem.badgeValue = @"3";

    

    UIImage *image1 = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIImage *image2 = [[UIImage imageNamed:@"Two30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

    three.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:image1 selectedImage:image2];

    

    

//第四个

    FourViewController *fourVC = [FourViewController new];

    UINavigationController *four = [[UINavigationController alloc]initWithRootViewController:fourVC];

    

    four.tabBarItem.title = @"";

    four.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    four.tabBarItem.badgeValue = @"4";

//第五个

    FiveViewController *fiveVC = [FiveViewController new];

    UINavigationController *five = [[UINavigationController alloc]initWithRootViewController:fiveVC];

    

    //five.tabBarItem.title = @"";

    //five.tabBarItem.image = [[UIImage imageNamed:@"han30.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    five.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:image1 tag:1005];

    five.tabBarItem.badgeValue = @"5";

    

 //第六个

    SixViewController *sixVC = [SixViewController new];

    UINavigationController *six = [[UINavigationController alloc]initWithRootViewController:sixVC];

    six.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:image1 tag:1006];

    six.tabBarItem.badgeValue = @"6";

    

    

    

    

    

    

    

        //将标签加入到控制器中

    self.viewControllers = @[one,two,three,four,five,six];

        //设置标签字体颜色

    self.tabBar.tintColor = [UIColor redColor];

        //设置标签视图控制器的颜色

    self.tabBar.barTintColor = [UIColor yellowColor];

        //设置默认显示第几个

    self.selectedIndex = 2;

        //设置是否半透明

    self.tabBar.translucent = YES;

    

    

    

    

    

}










- (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





#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}


- (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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值