简体   繁体   English

iOS导航标题不显示在子页面上

[英]iOS navigation title doesn't show on subpage

I have an app tab based with a navigation controller, 我有一个基于导航控制器的应用程序选项卡,

the title in the navigation bar shows fine in the root page for the tab, 导航栏中的标题在选项卡的根页面中显示正常,

but when I push another view controller, on top, I cannot set the title for this view 但是当我按下另一个视图控制器时,我无法为此视图设置标题

this is the code I use for the root page and the pushed page, 这是我用于根页面和推送页面的代码,

- (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;

 }

so, what is missing to set my title on the nav bar? 那么,在导航栏上设置我的标题缺少什么?

thanks! 谢谢!

你应该在新的控制器viewDidLoad中更改标题

You could try self.navigationController.navigationItem.titleView 你可以试试self.navigationController.navigationItem.titleView

Or if you use iOS5 or later.. 或者,如果您使用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];

Call this once in viewDidLoad in your rootViewController and then you can just use self.title=@"foo" everywhere. 在rootViewController中的viewDidLoad中调用一次,然后你可以在任何地方使用self.title=@"foo"

CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview: lava];
self.navigationItem.titleView = newView;

Try like this. 试试这样吧。 I think it will be helpful to you. 我认为这对你有所帮助。

What are you using to push the view controller on top? 你用什么来推动视图控制器?

There is a difference between: 有以下区别:

[self.navigationController pushViewController:viewController animated:YES];

and

[self.navigationController presentModalViewController:viewController animated:YES];

so if you're using the presentModalViewController method, it won't show your title. 因此,如果您使用presentModalViewController方法,它将不会显示您的标题。

If that's not the issue and you're using the pushViewController method, you could try setting the title of the view controller you're pushing. 如果这不是问题,并且您正在使用pushViewController方法,则可以尝试设置您正在推送的视图控制器的标题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Controller title";
}

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

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