用navgationController和tabbarViewController創(chuàng)建

多個子vc公有一個navgationController,一個tabbarController

1.自定義個navgationController,是window的rootVC,navgationVC的rootVC是TabVC

// 在navigationController.m中
- (void)setRootViewControllerWithNavigation{
    _tab = [[UITabBarController alloc]init];
    
    MainViewController *mainViewController = [MainViewController new];
 
    NewsViewController *activeViewController = [NewsViewController new];
  
    ManagerViewController *managerViewController = [ManagerViewController new];

    StoreViewController *storeViewController = [StoreViewController new];
   
    MineViewController *mineViewController = [MineViewController new];
  
    mainViewController.title = @"首頁";
    activeViewController.title = @"資訊";
    managerViewController.title = @"游戲管理";
    storeViewController.title = @"商城";
    mineViewController.title = @"我的";
    _tab.viewControllers = @[mainViewController,activeViewController,storeViewController,mineViewController];
    self.navigationController = [[BLNavigationController alloc]initWithRootViewController:_tab];
    
    self.appWindow.rootViewController = self.navigationController;
}
// 在applegate中調用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [[BLViewControllerManager sharedManager] setRootViewControllerWithNavigation];
    return YES;
}

2.不需要navgationBar的在vc的viewWillAppear方法中隱藏,隱藏后所有vc的navgationBar都會被隱藏,所以需要的vc中的viewWillAppear中設置為NO;其他屬性也可以在該方法中區(qū)分

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

3.給vc設置navgationItem標題時按普通方法無法顯示標題
self.navigationController.navigationItem.title = @"資訊中心";
,所以需要這樣寫

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.navigationItem.title = @"資訊中心";
    self.tabBarController.navigationItem.rightBarButtonItem = nil;
    self.tabBarItem.badgeValue = @"1";    // tabar上的小紅點數字,類似微信未讀數
}

每個子vc有自己的navgationController,共有一個tabbarController

1 . 自定義TabbarVC,是window的rootVC,TabbarVC的viewcontrollers是navgationVC數組

// 在自定義的TabbarVC中
-(void)createTabBar{
    NewsViewController * news = [[NewsViewController alloc] init];
    HeroViewController * hero = [[HeroViewController alloc] init];
    FindViewController * find = [[FindViewController alloc] init];
    MEViewController * me = [[MEViewController alloc] init];
    
    NSMutableArray * array = [NSMutableArray arrayWithObjects:news,hero,find,me, nil]; // 不要用不可變的給可變的賦值
    NSArray * titleArr = @[@"新聞",@"英雄",@"發(fā)現",@"我"];
    NSArray * normalArr = @[@"tab_icon_news_normal@2x",@"tab_icon_friend_normal@2x",@"tab_icon_quiz_normal@2x",@"tab_icon_more_normal@2x"];
    NSArray * selectedArr = @[@"tab_icon_news_press@2x",@"tab_icon_friend_press@2x",@"tab_icon_quiz_press@2x",@"tab_icon_more_press@2x"];
    // 標簽欄控制器
    for (int i = 0; i< array.count; i ++) {
        // 得到每個視圖控制器
        UIViewController * vc = array[i];
        // 視圖控制器 --> 導航控制器
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
        if (i == 2) {
            nav.navigationBarHidden = YES;
        }
        // 替換數組(視圖控制器 --> 導航控制器)
        [array replaceObjectAtIndex:i withObject:nav];
        // 標題
        vc.title = titleArr[i];
        // *渲染模式 : 保證顯示圖片與給定圖片色調一致
        UIImage * normalImage = [UIImage imageNamed:normalArr[i]];
        UIImage * selectedImage = [UIImage imageNamed:selectedArr[i]];
        // 進行渲染后賦值
        nav.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    self.viewControllers = array;
}
// 在applegate中調用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
  _window.rootViewController = [[MainTabBarViewController alloc] init];
    return YES;
}

每個子VC的navgation不會互相影響都可以隨便設置屬性了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容