繁体   English   中英

导航栏不显示标题

[英]Navigation Bar not showing title

我无法获得导航栏来显示我指定的标题。 我试图更改AppDelegate.m中以及第一个选项卡视图的viewDidLoad中的标题。 我怀疑标题被隐藏了,但无法修复。 请提供您可能有的任何见解。

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];


    UIColor *firstColor = [UIColor colorWithRed:155.0f/255.0f green:41.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
    UIColor *secondColor = [UIColor colorWithRed:215.0f/255.0f green:90.0f/255.0f blue:18.0f/255.0f alpha:1.0f];

    NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];

    [[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];

    self.navigationController.title = @"This title is not showing";

    [[navigationController navigationBar] setTranslucent:NO]; 


    //create the view controller for the first tab
    self.firstViewController = [[FirstViewController alloc] initWithNibName:nil
                                                                     bundle:NULL];

    //create the view controller for the second tab
    self.secondViewController = [[SecondViewController alloc] initWithNibName:nil
                                                                       bundle:NULL];

    //create the view controller for the third tab
    self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil
                                                                       bundle:NULL];

    //create the view controller for the fourth tab
    self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil
                                                                     bundle:NULL];

    //create an array of all view controllers that will represent the tab at the bottom
    NSArray *myViewControllers = [[NSArray alloc] initWithObjects:
                                  self.firstViewController,
                                  self.secondViewController, self.thirdViewController, self.fourthViewController, nil];

    //initialize the tab bar controller
    self.tabBarController = [[MainUITabBarController alloc] init];

    //set the view controllers for the tab bar controller
    [self.tabBarController setViewControllers:myViewControllers];


    [navigationController setViewControllers:@[self.tabBarController]];

    [self.window setRootViewController:navigationController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];  

    return YES;

}

您的视图控制器的关系不正确。

  1. 应用程序的根视图控制器应为TabBarController。

  2. 每个项目都应具有自己的导航控制器。

  3. 每个ViewController的标题都可以在viewDidLoad()使用self.title = "...

我已经能够按如下所示在委托中设置标题:1.)创建视图控制器2.)设置那些标题3.)然后创建UINavigationControllers并在那里分配视图控制器

self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
self.secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil bundle:nil];

[self.firstViewController setTitle:@"First"];
[self.secondViewController setTitle:@"Second"];
[self.thirdViewController setTitle:@"Third"];
[self.fourthViewController setTitle:@"Fourth"];

UINavigationController *controller1 = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
UINavigationController *controller2 = [[UINavigationController alloc] initWithRootViewController:self.secondViewController];
UINavigationController *controller3 = [[UINavigationController alloc] initWithRootViewController:self.thirdViewController];
UINavigationController *controller4 = [[UINavigationController alloc] initWithRootViewController:self.fourthViewController];

NSArray *viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM