简体   繁体   中英

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

Or if you use iOS5 or later..

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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