iOS導航控制器和標籤欄控制器的結合

來源:互聯網
上載者:User

標籤:ios   uinavigationcontorll   uitabbarcontroller   

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



<span style="font-family: Arial, Helvetica, sans-serif;">在程式啟動時設定根控制器為 tabbar,如下</span>

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

{

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];


    [selfinitTabBar];

    self.window.rootViewController =self.tabBar;

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}



iOS導航控制器和標籤欄控制器的結合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.