UITabBarController使用

来源:互联网 发布:知乎 横渡太平洋 编辑:程序博客网 时间:2024/06/05 15:25

参考文献: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 = [UIColorblackColor];

    

    

    

    //改变标题颜色

    [[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:firstImageselectedImage: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:secondImageselectedImage: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:threeImageselectedImage: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:fourImageselectedImage: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:fiveImageselectedImage: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的导航视图给覆盖

0 0
原创粉丝点击