iOS导航标题未显示在子页面上
我有一个基于导航控制器的应用程序选项卡 导航栏中的标题在选项卡的根页面中显示良好 但当我在顶部按下另一个视图控制器时,我无法设置此视图的标题 这是我用于根页面和推送页面的代码iOS导航标题未显示在子页面上,ios,uinavigationbar,title,Ios,Uinavigationbar,Title,我有一个基于导航控制器的应用程序选项卡 导航栏中的标题在选项卡的根页面中显示良好 但当我在顶部按下另一个视图控制器时,我无法设置此视图的标题 这是我用于根页面和推送页面的代码 - (void)viewDidLoad { [super viewDidLoad]; UILabel *lava = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]autorelease]; lava.backgroundColo
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *lava = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]autorelease];
lava.backgroundColor = [UIColor clearColor];
lava.text = @"About us";
lava.textAlignment = UITextAlignmentCenter ;
lava.textColor = [UIColor whiteColor];
lava.font = [UIFont fontWithName:@"DroidSans-Bold" size:(46)];
lava.shadowColor = [UIColor blackColor];
lava.shadowOffset = CGSizeMake(2, 3);
self.navigationItem.titleView = lava;
}
那么,在导航栏上设置标题缺少什么呢
谢谢 您应该在新控制器视图DIDLOAD中更改标题您应该在新控制器视图DIDLOAD中更改标题您可以尝试self.navigationController.navigationItem.titleView 或者如果您使用iOS5或更高版本
UIImage *img=[UIImage imageNamed:@"NavBarBackground.png"];
[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0f],UITextAttributeFont
,[UIColor whiteColor],UITextAttributeTextColor
,[UIColor blackColor],UITextAttributeTextShadowColor
,[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset
, nil];
[self.navigationController.navigationBar setTitleTextAttributes:dict];
在rootViewController的viewDidLoad中调用此函数一次,然后您可以使用self.title=@foo everywhere。您可以尝试self.navigationController.navigationItem.titleView
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview: lava];
self.navigationItem.titleView = newView;
或者如果您使用iOS5或更高版本
UIImage *img=[UIImage imageNamed:@"NavBarBackground.png"];
[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0f],UITextAttributeFont
,[UIColor whiteColor],UITextAttributeTextColor
,[UIColor blackColor],UITextAttributeTextShadowColor
,[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset
, nil];
[self.navigationController.navigationBar setTitleTextAttributes:dict];
在rootViewController的viewDidLoad中调用一次,然后就可以使用self.title=@foo everywhere
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview: lava];
self.navigationItem.titleView = newView;
像这样试试。我想这对你会有帮助的
像这样试试。我认为这会对您有所帮助。您使用什么将视图控制器推到顶部 这两者之间有区别:
[self.navigationController pushViewController:viewController animated:YES];
及
因此,如果您使用的是presentModalViewController方法,它将不会显示您的标题
如果这不是问题所在,并且您使用的是pushViewController方法,则可以尝试设置要推送的视图控制器的标题
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Controller title";
}
您使用什么将视图控制器推到顶部 这两者之间有区别:
[self.navigationController pushViewController:viewController animated:YES];
及
因此,如果您使用的是presentModalViewController方法,它将不会显示您的标题
如果这不是问题所在,并且您使用的是pushViewController方法,则可以尝试设置要推送的视图控制器的标题
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Controller title";
}
你是在viewDidLoad中调用代码还是在其他地方调用代码?@xapslock是的,在viewDidLoad中,我编辑了问题Thanks是什么NSLog@Nav. 项目:%@,self.navigationItem;说?@PhillipMills谢谢你的根页面:Nav。项目:;子页面:导航。物品:你是在viewDidLoad中调用代码还是在其他地方调用代码?@xapslock是在viewDidLoad中,我编辑了问题Thanks是什么NSLog@Nav. 项目:%@,self.navigationItem;说?@PhillipMills谢谢你的根页面:Nav。项目:;子页面:导航。是的,谢谢,我有每个视图控制器的标题,这就是你的意思吗?试试self.navigationBar.topItem.titleView=lava,还要确保它不是nilyes谢谢,我有每个视图控制器的标题,这就是你的意思吗?试试self.navigationBar.topItem.titleView=lava,还要确保它不是零