TabViewController code only version

设置编程Tab栏视图控制器
本文指导如何在应用启动时初始化窗口、创建并配置Tab栏控制器,包括添加视图控制器、设置Tab栏属性和图标,以及指定根视图控制器。

In order to work with my colleagues, There are few steps to get a programmingly tabviewcontroller




adding the following steps in appdelegate.m  

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

//method


1.initiate window

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


2 initiate ViewController and tabbarcontroller

   UIViewController *blankVC = [UIViewController new];

    UIViewController *blankVC2 = [UIViewController new];

    UIViewController *blankVC3 = [UIViewController new];

    UIViewController *blankVC4 = [UIViewController new];

    UIViewController *blankVC5 = [UIViewController new];

    self.tabBarController = [[UITabBarController alloc]init];


3.adding VCs in Tabcontroller by NSarray

    

    NSArray* controllerArray = [[NSArray alloc]initWithObjects:blankVC,blankVC2,blankVC3,blankVC4,blankVC5 ,nil];

    self.tabBarController.viewControllers = controllerArray;


4.initiate tabBar

   UITabBar *tabBar = self.tabBarController.tabBar;

    [tabBar setTintColor: [UIColor colorWithRed:255.f/255.f green:135.f/255.f blue:5.f/255.f alpha:1]];

    [tabBar setBackgroundColor:[UIColor grayColor]];

    [self.tabBarController.tabBar setTranslucent:NO];



(5.set tabBar fonts Attribute)

    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:14],NSFontAttributeName, nil];

    [[UITabBarItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

    NSDictionary *attributesHL=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255.f/255.f green:135.f/255.f blue:5.f/255.f alpha:1],NSForegroundColorAttributeName,[UIFont systemFontOfSize:14],NSFontAttributeName, nil];

    [[UITabBarItem appearance] setTitleTextAttributes:attributesHL forState:UIControlStateHighlighted];


6.initiate tabbaritem and link them to tabBar

    

    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:0];

    UIImage* tabBarItem2Image = [UIImage imageNamed:@"tab_icons_one"];

    UIImage* tabBarItem2SelectedImage = [UIImage imageNamed:@"tab_icons_one_prs"];

    (void)[tabBarItem2 initWithTitle:@"first" image:tabBarItem2Image selectedImage:tabBarItem2SelectedImage];


    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:1];

    (void)[tabBarItem1 initWithTitle:@"" image:nil selectedImage:nil];

    tabBarItem1.enabled = false;


    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

    UIImage* tabBarItem3Image = [UIImage imageNamed:@"tab_icons_two"];

    UIImage* tabBarItem3SelectedImage = [UIImage imageNamed:@"tab_icons_two_prs"];

    (void)[tabBarItem3 initWithTitle:@"thrid" image:tabBarItem3Image selectedImage:tabBarItem3SelectedImage];

    //blank one

    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

    (void)[tabBarItem4 initWithTitle:@"" image:nil selectedImage:nil];

  tabBarItem4.enabled = false;


    UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];

    UIImage* tabBarItem5Image = [UIImage imageNamed:@"tab_icons_three"];

    UIImage* tabBarItem5SelectedImage = [UIImage imageNamed:@"tab_icons_three_prs"];

    (void)[tabBarItem5 initWithTitle:@"fifth image:tabBarItem5Image selectedImage:tabBarItem5SelectedImage];

(7.repositio tabbaritems)

    tabBarItem5.titlePositionAdjustment=UIOffsetMake(-40, -3.0);

    tabBarItem3.titlePositionAdjustment=UIOffsetMake(0, -3.0);

    tabBarItem2.titlePositionAdjustment=UIOffsetMake(40, -3.0);


8.set tabBar target to any viewcontroller in this case 

    self.tabBarController.selectedIndex = 1;


9.set tabbarcontroller the rootviewcontroller

    

    [self.window setRootViewController:self.tabBarController];

    [self.window makeKeyAndVisible];




all in all my intent here is to set the second invisible viewcontroller as Home but not people not able to tab on it.

It is our requirement to do so, you may not use the structure like this. so use it weathly

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值