[英]TableViewController is causing TabBar item title to change to navigation bar title
[英]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.