如何设置UINavigationBar的标题

时间:2014-04-25 01:29:33

标签: ios objective-c uiview uiviewcontroller uinavigationcontroller

我有一个基于标签的应用程序,我正在尝试更改UINavigationBar的标题,当点击一个按钮加载一个新的Uiview但没有显示!

我创建了这样的栏:

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

然后在方法中我尝试改变:

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

2 个答案:

答案 0 :(得分:1)

您可以创建一个UINavigationController并将其用作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];

}

覆盖viewWillAppear

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

答案 1 :(得分:1)

您没有看到UINavigationBar标题的原因是因为导航条没有任何内容,因此topItemnil

您需要创建并设置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];

当然,将视图控制器放在UINavigationController中可能更好一点,但上面解释了问题的原因。