前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >iOS结合导航控制器和标签栏控制器

iOS结合导航控制器和标签栏控制器

作者头像
全栈程序员站长
发布2022-07-06 18:22:59
发布2022-07-06 18:22:59
60300
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是全栈君

代码语言:javascript
代码运行次数:0
运行
复制
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="objc">- (void)initTabBar
{
    //    初始化主页
    HomePageViewController *homePage = [[HomePageViewController alloc]init];
    UINavigationController *homePageNav = [[UINavigationController alloc]initWithRootViewController:homePage];

    //    推断是否存在用户存储的城市
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if (![userDefault objectForKey:@"cityDic"]) {
        CitySelectionViewController *citySelection = [[CitySelectionViewController alloc]init];
        [homePageNav pushViewController:citySelection animated:YES];
    }
    // 设置主页-TabBar
    UITabBarItem *itemHomePage= [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.png"] scaledToSize:CGSizeMake(25, 25)] tag:0];
    [itemHomePage setFinishedSelectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.jpg"] scaledToSize:CGSizeMake(25, 25)] withFinishedUnselectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-2.jpg"] scaledToSize:CGSizeMake(25, 25)]];
    homePageNav.tabBarItem=itemHomePage;
    [itemHomePage setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NAVBACKGROUNDCOLOR,UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO([NSString stringWithFormat:@"7.0"])) {
        [homePageNav.navigationBar setBarTintColor:NAVBACKGROUNDCOLOR];
    }else
    {
        [homePageNav.navigationBar setTintColor:NAVBACKGROUNDCOLOR];
    }
    homePageNav.navigationBar.translucent = NO;
    
    
//    初始化我的购物车
    ShoppingCartViewController *shoppingCart = [[ShoppingCartViewController alloc]init];
    UINavigationController *shoppingCartNav = [[UINavigationController alloc]initWithRootViewController:shoppingCart];

    // 设置购物车-TabBar
    UITabBarItem *itemShoppingCart= [[UITabBarItem alloc] initWithTitle:@"我的购物车" image:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.png"] scaledToSize:CGSizeMake(25, 25)] tag:0];
    [itemShoppingCart setFinishedSelectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.jpg"] scaledToSize:CGSizeMake(25, 25)] withFinishedUnselectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-2.jpg"] scaledToSize:CGSizeMake(25, 25)]];
    shoppingCartNav.tabBarItem=itemShoppingCart;
    [itemShoppingCart setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NAVBACKGROUNDCOLOR,UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO([NSString stringWithFormat:@"7.0"])) {
        [shoppingCartNav.navigationBar setBarTintColor:NAVBACKGROUNDCOLOR];
    }else
    {
        [shoppingCartNav.navigationBar setTintColor:NAVBACKGROUNDCOLOR];
    }
    shoppingCartNav.navigationBar.translucent = NO;
    
    //    初始化我的汇众
    IndividualCenterViewController *individualCenter = [[IndividualCenterViewController alloc]init];
    UINavigationController *individualCenterNav = [[UINavigationController alloc]initWithRootViewController:individualCenter];
    
    // 设置汇众-TabBar
    UITabBarItem *itemindividualCenter= [[UITabBarItem alloc] initWithTitle:@"我的幸福家" image:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.png"] scaledToSize:CGSizeMake(25, 25)] tag:0];
    [itemindividualCenter setFinishedSelectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.jpg"] scaledToSize:CGSizeMake(25, 25)] withFinishedUnselectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-2.jpg"] scaledToSize:CGSizeMake(25, 25)]];
    individualCenterNav.tabBarItem=itemindividualCenter;
    [itemindividualCenter setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NAVBACKGROUNDCOLOR,UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO([NSString stringWithFormat:@"7.0"])) {
        [individualCenterNav.navigationBar setBarTintColor:NAVBACKGROUNDCOLOR];
    }else
    {
        [individualCenterNav.navigationBar setTintColor:NAVBACKGROUNDCOLOR];
    }
    individualCenterNav.navigationBar.translucent = NO;
    
    self.tabBar = [[UITabBarController alloc] init];
    [[UITabBar appearance] setBackgroundImage:[UIImage setBackgroundImageByColor:COLOR(255, 255, 255, 1) withFrame:RectWithPara(0, 0, SCREEN_WIDTH, 44)]];  // 设置UITabBar背景图片
    [[UITabBar appearance] setSelectionIndicatorImage:
    [UIImage imageWithTint:COLOR(235, 235, 235, 1)]];
    self.tabBar.viewControllers = [NSArray arrayWithObjects:homePageNav,shoppingCartNav,individualCenterNav, nil];
}
代码语言:javascript
代码运行次数:0
运行
复制
<span style="font-family: Arial, Helvetica, sans-serif;">在程序启动时设置根控制器为 tabbar,例如以下</span>

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

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

[self initTabBar];

self.window.rootViewController = self.tabBar;

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

return YES;

}

版权声明:本文博主原创文章。博客,未经同意不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116855.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档