簡體   English   中英

單擊iOS中的選項卡項時,無法在導航欄中設置標題

[英]Can not set title in Navigation bar when click on tabbar item in iOS

我嘗試在UIViewcontroller中創建UItabbarcontroller。 但是,當我單擊選項卡項(firstview)時,它在根viewcontroller的頂部顯示了導航欄。 我設置了標題,但未顯示。

代碼創建UITabBarcontroller:

self.tab=[[UITabBarController alloc]init];

// FirstViewController
First *fvc=[[First alloc]initWithNibName:nil bundle:nil];
fvc.title=@"First";
fvc.tabBarItem.image=[UIImage imageNamed:@"i.png"];

//SecondViewController
Second *svc=[[Second alloc]initWithNibName:nil bundle:nil];
svc.title=@"Second";
svc.tabBarItem.image=[UIImage imageNamed:@"im.png"];

//ThirdViewController
Third *tvc=[[Third alloc]initWithNibName:nil bundle:nil];
tvc.title=@"Third";
tvc.tabBarItem.image=[UIImage imageNamed:@"img.png"];

self.tab.viewControllers=[NSArray arrayWithObjects:fvc, svc, tvc, nil];

[self.view addSubview:self.tab.view];

和在Firstviewcontroller中的代碼:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    userLogin1 =[[NSUserDefaults standardUserDefaults]valueForKey :@"textfieldtext"];
    NSLog(@"User login: %@",userLogin1);

    self.navigationItem.title = [NSString stringWithFormat: @"Hello, %@",userLogin1]; // it not works

}

請看這張圖片: 在此處輸入圖片說明 它只顯示按鈕Back,不顯示標題。 我不知道為什么

像這樣設置:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    userLogin1 =[[NSUserDefaults standardUserDefaults]valueForKey :@"textfieldtext"];
    NSLog(@"User login: %@",userLogin1);
//Here
    self.title = [NSString stringWithFormat: @"Hello, %@",userLogin1]; // it not works

}

看起來您正在將UITabBarController添加到UINavigationController ,那么您需要執行以下操作:

self.parentViewController.navigationItem.title = [NSString stringWithFormat: @"Hello, %@",userLogin1];

如果要在導航欄中設置圖像或顏色,請使用此代碼。

     // For Background Image
     [[UINavigationBar appearance] setBackgroundImage:[UIImage 
     imageNamed:@"TitleImage.png"]] forBarMetrics:UIBarMetricsDefault];

     // For Background Color
     [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor 
     whiteColor]] forBarMetrics:UIBarMetricsDefault];

對於導航欄標題,請使用此代碼...

     self.title = NSLocalizedString(@"FirstViewControllerTitle",nil);

                                     OR

     self.title = @"FirstViewControllerTitle";

UItabbarcontroller為每個ViewController設置UINavigationController

UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:controller_1];

並像這樣為每個ViewController設置標題

self.title = @"yourTitle";

你可以這樣設置

[self.tabBarController setTitle:@"Title"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM