标签:
参考文献: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;
}
实现的效果图如下:
其中关键的代码是:self.window.rootViewController = self.mainTabBarController;
我们习惯的写法是:self.window.rootViewController = [[UINavigationControlleralloc]initWithRootViewController:self.mainTabBarController];
按照习惯的写法就会导致我们无法正常使用自己写的导航视图UINavigationController对象,而会被UITabBarController的导航视图给覆盖
标签:
原文地址:http://blog.csdn.net/junk1357/article/details/51351683