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

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

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

- (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];
}




在程序启动时设置根控制器为 tabbar,如下

– (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]美高梅59599,;

[self.window makeKeyAndVisible];

return YES;

}

http://www.bkjia.com/IOSjc/898409.htmlwww.bkjia.comtruehttp://www.bkjia.com/IOSjc/898409.htmlTechArticleiOS导航控制器和标签栏控制器的结合
(void)initTabBar{ // 初始化主页 HomePageViewController *homePage =
[[HomePageViewController alloc]init]; UINavigationControl…

使用一个控制器A去管理多个控制器,如B,C;控制器A称为父控制器,B,C称为子控制器;

iOS提供了这两个比较特殊的控制器,根据需要可以自定义;

UINavigationController:

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

{

// Override point for customization after application launch.

// 创建窗口

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

// 窗口背景色

_window.backgroundColor = [UIColor whiteColor];

// 实例控制器 a

AViewController *a = [[AViewController alloc] init];

// 实例导航控制器 nav

UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:a];

// 设置根控制器为nav

self.window.rootViewController = nav;

//

[_window makeKeyAndVisible];

return YES;

}

UITabBarController:

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

admin

网站地图xml地图