UITabBarController使用

参考文献: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的导航视图给覆盖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lock颠颠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值