开发者

UINavigationBar displays incorrectly when combine with UITabBarController

开发者 https://www.devze.com 2023-04-07 11:01 出处:网络
I\'m trying to create Home Screen for UITabBarViewController with another UINavigationViewController and UIViewController Subclass.

I'm trying to create Home Screen for UITabBarViewController with another UINavigationViewController and UIViewController Subclass.

In application,There are:

  • two Tab for loading NewsController and VideoController
  • HomeViewController that loads immediately when application finish launch.

This is my application shot screen.

HomeViewController

UINavigationBar displays incorrectly when combine with UITabBarController

NavigationBar show a half

NewsViewController

UINavigationBar displays incorrectly when combine with UITabBarController

This is my code.

//In TabBarWithHomeDelegate.m

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

    homeViewController = [[HomeViewController alloc]init];

    UINavigationController *nav = [[UINavigationController alloc]init];

    nav.navigationItem.title = @"Tab 1 Data";
    [nav pushViewController:homeViewController animated:NO]; 


    [self.t开发者_StackOverflow中文版abBarController setSelectedViewController:nav];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

//In NewsViewController.m for touching on home button

-(IBAction) homeButtonClick:(id)sender
{
        TabBarWithHomeAppDelegate * appDelegate 
        = [[UIApplication sharedApplication] delegate];

        UITabBarController * tabBarController =  appDelegate.tabBarController;
        [tabBarController setSelectedViewController:nil];
        [tabBarController setSelectedViewController:appDelegate.homeViewController];

}

In addition, I have attached source code. I'm will be grad if you see that and help me to solve this. In fact, I try to do it myself almost 6 hours.

link to download source code.


Your HomeViewController is not assigned as a tab in your UITabBarController, so you should not call:

[tabBarController setSelectedViewController:appDelegate.homeViewController];

You should either make it a real tab or do something different. I would recommend calling

[tabBarController presentModalViewController:homeViewController animated:YES];

You will not be able to see the tab bar in this scenario so you will need a different way to dismiss the homeViewController. However, this is more correct as it doesn't really make sense for the user to see a tab bar controller with no tabs currently selected.


I just comment you code in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions and all works perfect:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号