【问题标题】:iOS navigation title doesn't show on subpageiOS 导航标题不显示在子页面上
【发布时间】:2012-07-02 12:01:09
【问题描述】:

我有一个基于导航控制器的应用标签,

导航栏中的标题在选项卡的根页面中显示良好,

但是当我在顶部推送另一个视图控制器时,我无法设置此视图的标题

这是我用于根页面和推送页面的代码,

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

 }

那么,在导航栏上设置我的标题缺少什么?

谢谢!

【问题讨论】:

  • 你是在 viewDidLoad 还是其他地方调用你的代码?
  • @xapslock 在 vi​​ewDidLoad 中是的,我编辑了这个问题,谢谢
  • NSLog(@"Nav. item: %@", self.navigationItem); 说什么?
  • @PhillipMills 感谢您的根页面:Nav。项目:;和子页面:导航。项目:

标签: ios uinavigationbar title


【解决方案1】:

你可以试试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"

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,谢谢,我有每个视图控制器的标题,这是您的意思吗?
    • 试试 self.navigationBar.topItem.titleView = lava ; , 还要确保它不是 nil
    【解决方案3】:
    CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
    UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
    [newView addSubview: lava];
    self.navigationItem.titleView = newView;
    

    试试这样。我想这对你会有帮助。

    【讨论】:

      【解决方案4】:

      你用什么将视图控制器推到顶部?

      两者之间有区别:

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

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

      所以如果你使用 presentModalViewController 方法,它不会显示你的标题。

      如果这不是问题并且您正在使用 pushViewController 方法,您可以尝试设置您正在推送的视图控制器的标题。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 2014-04-21
        相关资源
        最近更新 更多