码迷,mamicode.com
首页 > 其他好文 > 详细

UITabBarController使用

时间:2016-05-12 20:41:40      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

参考文献:http://blog.csdn.net/lovefqing/article/details/8255846


UITabBarController使用是没什么问题,问题是如何在使用的过程中能正常使用导航视图,正常添加代码是无法实现的,需要自己在每个页面添加一个导航视图UINavigationController;


具体代码如下:

@interface AppDelegate ()


@property (nonatomic,strong) UITabBarController* mainTabBarController;


@end


@implementation AppDelegate


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

    

    UCFirstViewController* firstView = [[UCFirstViewControlleralloc] init];

    UINavigationController* firstNavi = [[UINavigationControlleralloc] initWithRootViewController:firstView];

    

    UCSecondViewController* secondView = [[UCSecondViewControlleralloc] init];

    UINavigationController* secondNavi = [[UINavigationControlleralloc] initWithRootViewController:secondView];

    

    UCThreeViewController* threeView = [[UCThreeViewControlleralloc] init];

    UINavigationController* threeNavi = [[UINavigationControlleralloc] initWithRootViewController:threeView];

    

    UCFourViewController* fourView = [[UCFourViewControlleralloc] init];

    UINavigationController* fourNavi = [[UINavigationControlleralloc] initWithRootViewController:fourView];

    

    UCFiveViewController* fiveView = [[UCFiveViewControlleralloc] init];

    UINavigationController* fiveNavi = [[UINavigationControlleralloc] initWithRootViewController:fiveView];

    

    self.mainTabBarController = [[UITabBarControlleralloc] init];

    self.mainTabBarController.viewControllers = [[NSArrayalloc] initWithObjects:firstNavi,secondNavi,threeNavi,fourNavi,fiveNavi,nil];

    self.mainTabBarController.tabBar.barTintColor = [UIColor blackColor];

    

    

    

    //改变标题颜色

    [[UITabBarItemappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:

                                                       [UIColorwhiteColor], NSForegroundColorAttributeName,

                                                       nil]forState:UIControlStateNormal];

    UIColor *titleHighlightedColor = [[UCToolsharedServer] colorWithHexString:@"ff4500"];

    [[UITabBarItemappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:

                                                       titleHighlightedColor,NSForegroundColorAttributeName,

                                                       nil]forState:UIControlStateSelected];

    

    //标签声明

    UIImage* firstImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/first"];

    UIImage* firstSelectImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/first_pre"];

    firstSelectImage = [firstSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UITabBarItem* firstItem = [[UITabBarItemalloc] initWithTitle:@"精选"image:firstImage selectedImage:firstSelectImage];

    firstNavi.tabBarItem = firstItem;

    

    UIImage* secondImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/second"];

    UIImage* secondSelectImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/second_pre"];

    secondSelectImage = [firstSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UITabBarItem* secondItem = [[UITabBarItemalloc] initWithTitle:@"发现"image:secondImage selectedImage:secondSelectImage];

    secondNavi.tabBarItem = secondItem;

    

    UIImage* threeImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/three"];

    UIImage* threeSelectImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/three_pre"];

    threeSelectImage = [firstSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UITabBarItem* threeItem = [[UITabBarItemalloc] initWithTitle:@"发布"image:threeImage selectedImage:threeSelectImage];

    threeNavi.tabBarItem = threeItem;

    

    UIImage* fourImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/four"];

    UIImage* fourSelectImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/four_pre"];

    fourSelectImage = [firstSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UITabBarItem* fourItem = [[UITabBarItemalloc] initWithTitle:@"管理"image:fourImage selectedImage:fourSelectImage];

    fourNavi.tabBarItem = fourItem;

    

    UIImage* fiveImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/five"];

    UIImage* fiveSelectImage = [[UCToolsharedServer] getImageFromTBBundleWithImageName:@"tabbar/five_pre"];

    fiveSelectImage = [firstSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UITabBarItem* fiveItem = [[UITabBarItemalloc] initWithTitle:@"我的"image:fiveImage selectedImage:fiveSelectImage];

    fiveNavi.tabBarItem = fiveItem;

    

    self.window.rootViewController = self.mainTabBarController;

    

    [self.windowmakeKeyAndVisible];

    returnYES;

}



实现的效果图如下:

file:///Users/slink/Library/Containers/com.tencent.qq/Data/Library/Application%20Support/QQ/Users/671393422/QQ/Temp.db/47AE6A28-00DA-480D-874E-87C1A6664312.png

其中关键的代码是:self.window.rootViewController = self.mainTabBarController;

我们习惯的写法是:self.window.rootViewController = [[UINavigationControlleralloc]initWithRootViewController:self.mainTabBarController];

按照习惯的写法就会导致我们无法正常使用自己写的导航视图UINavigationController对象,而会被UITabBarController的导航视图给覆盖

UITabBarController使用

标签:

原文地址:http://blog.csdn.net/junk1357/article/details/51351683

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!