Question

I have a tab based app and I am trying to change the title of the UINavigationBar, when a button is clicked which loads up a new Uiview but nothing shows !

I create the bar like this:

navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 60)];
navBar.barTintColor = [UIColor colorWithRed:0.082 green:0.118 blue:0.141 alpha:1.0];
navBar.tintColor = [UIColor whiteColor];
navBar.translucent = NO;
[self.view addSubview:navBar];

Then in the method i try change:

UIView *sendASongView = [[UIView alloc] init];
[sendASongView setFrame: CGRectMake(0, 60, screenWidth, screenHeight-110)];
[sendASongView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:sendASongView];
navBar.topItem.title = @"Send";
Était-ce utile?

La solution

You can create a UINavigationController and use it as the container of your viewController.

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {

    self.statusLabel.text = @"You're logged in as";

    HomeNavViewController *profileviewController = [[HomeNavViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: profileviewController] ;


    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:transition forKey:nil];
    [self presentModalViewController:nav animated:NO];

}

Override the method viewWillAppearin YourViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated] ;
    self.title = @"Sender" ;
}

Autres conseils

The reason you don't see a title with your UINavigationBar is because the navbar has no items so topItem is nil.

You need to create and set a UINavigationItem.

navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 60)];
navBar.barTintColor = [UIColor colorWithRed:0.082 green:0.118 blue:0.141 alpha:1.0];
navBar.tintColor = [UIColor whiteColor];
navBar.translucent = NO;

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Send"];
navBar.items = @[ item ];

UIView *sendASongView = [[UIView alloc] init];
[sendASongView setFrame: CGRectMake(0, 60, screenWidth, screenHeight-110)];
[sendASongView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:sendASongView];

Of course it is probably a better idea to put your view controller in a UINavigationController but the above explains the cause of your problem.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top