首页 > 代码库 > iOS 在TabViewController中设置Tab Bar,且实现自定义

iOS 在TabViewController中设置Tab Bar,且实现自定义

开发环境:Xcode 6.1 iOS 7.1.2 & iOS 8.1

实现功能一,在打开APP时即显示第三个Tab对应的View;

实现功能二,给每一个Tab设置图片和选中图片。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //---------------------------------------------
    // set the first view as camera view and custom tab bar
    //---------------------------------------------
    [self setTabBar];
}

实现setTab Bar:
//---------------------------------------------
#pragma mark - Tab Bar
//---------------------------------------------
- (void) setTabBar{
    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;
    [tabViewController setSelectedIndex:2];

    UITabBar *tabBar = tabViewController.tabBar;
    [tabBar setTintColor: [UIColor greenColor]];
    
    // First
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UIImage* tabBarItem1Image = [UIImage imageNamed:@"XXX"];
    UIImage *scaledTabBarItem1Image = [UIImage imageWithCGImage:[tabBarItem1Image CGImage] scale:(tabBarItem1Image.scale * 3) orientation:(tabBarItem1Image.imageOrientation)];
    UIImage* tabBarItem1SelectedImage = [UIImage imageNamed:@"XXX"];
    UIImage *scaledTabBarItem1SelectedImage = [UIImage imageWithCGImage:[tabBarItem1SelectedImage CGImage] scale:(tabBarItem1SelectedImage.scale * 3) orientation:(tabBarItem1SelectedImage.imageOrientation)];
    (void)[tabBarItem1 initWithTitle:@"First" image:scaledTabBarItem1Image selectedImage:scaledTabBarItem1SelectedImage];
    
    // Second
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UIImage* tabBarItem2Image = [UIImage imageNamed:@"XXX"];
    UIImage *scaledTabBarItem2Image = [UIImage imageWithCGImage:[tabBarItem2Image CGImage] scale:(tabBarItem2Image.scale * 3) orientation:(tabBarItem2Image.imageOrientation)];
    UIImage* tabBarItem2SelectedImage = [UIImage imageNamed:@"XXX"];
    UIImage *scaledTabBarItem2SelectedImage = [UIImage imageWithCGImage:[tabBarItem2SelectedImage CGImage] scale:(tabBarItem2SelectedImage.scale * 3) orientation:(tabBarItem2SelectedImage.imageOrientation)];
    (void)[tabBarItem2 initWithTitle:@"Second" image:scaledTabBarItem2Image selectedImage:scaledTabBarItem2SelectedImage];
    
    // And Others Tabs - 同样的方法
    
}

didFinishLaunchingWithOptions就自定义跳转直接到某一个Tab:

    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;
    [tabViewController setSelectedIndex:2];

为了保证像素,使图标比较清晰,使用了大的图片,然后用代码将其缩小:

[UIImage imageWithCGImage:[tabBarItem1Image CGImage] scale:(tabBarItem1Image.scale * 3) orientation:(tabBarItem1Image.imageOrientation)];
另:

因为selected image系统总是会将图片设置成和tint color一样颜色的图片,默认是蓝色,所以需要自己设置你需要的颜色:

[tabBar setTintColor: [UIColor greenColor]];


参考链接:

http://stackoverflow.com/questions/18894985/uitabbar-not-showing-selected-item-images-in-ios-7

http://stackoverflow.com/a/19386156/3458781

http://www.appcoda.com/ios-programming-how-to-customize-tab-bar-background-appearance/

iOS 在TabViewController中设置Tab Bar,且实现自定义