【问题标题】:put title for NavigationBar为 NavigationBar 放置标题
【发布时间】:2011-09-21 11:55:28
【问题描述】:

我有以下几点:

头文件:

 UINavigationBar *navigationBar;
  UINavigationItem *navigationItem;

实现文件: 在viewDidLoad 方法中我尝试了这些:

self.navigationBar.topItem.title = @"title text";

还有这个:

  self.navigationItem.title = @"MyTitle";
  [self.navigationBar pushNavigationItem:navigationItem animated:NO];

但黑条上似乎没有出现标题:

有人知道吗?谢谢! 编辑:

【问题讨论】:

  • 谁来创建导航栏?你如何看待这种观点?
  • 在viewDidLoad中试试请添加self.title=@"Title";
  • 为什么不在 Interface Builder 中设置标题?
  • 我有我的理由。每次加载时标题都会改变..:)
  • 有什么理由隐藏 viewController 的默认导航项?使用您自己的 UINavigationItem *navigationItem;

标签: iphone navigationbar


【解决方案1】:
  1. 你应该在你的类属性中定义:

    @property (nonatomic, assign) IBOutlet UINavigationBar *navigationBar;

  2. 您应该将该类设置为File's Owner。

  3. 您应该将导航栏与 1. 中的属性连接起来(使用 IBOutlet)
  4. 现在您可以使用:self.navigationBar.topItem.title = @"title text"; 设置其标题

【讨论】:

  • 不工作。有问题...即使我通过xib文件设置标题...我在模拟器上看不到它!
  • 控制器的 IBOutlet view 连接到 xib 中的哪个视图?
  • 工作 100% 如果你卡住这意味着你创建了 xib 文件复制其他文件。检查所有连接引用,如果您看到任何感叹号 (!),则表示出现问题。
【解决方案2】:

试试这个:-

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
label.font = [UIFont boldSystemFontOfSize:14.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
label.text = [dataItem objectForKey:@"title"];
[label release];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    相关资源
    最近更新 更多