【问题标题】:Want to change NavigationItem title想要更改 NavigationItem 标题
【发布时间】:2013-05-28 10:23:30
【问题描述】:

我已经将我的第一个视图嵌入到 NavigationController 中,然后我将 segue 设置为推送。 我想在每个视图中更改导航栏的标题以显示某些内容并通过我的代码对其进行更新。 我试图这样调用导航控制器:

self.navigationController.navigationBar.topItem.title = @"YourTitle";

但是这行不通,我该怎么办?

【问题讨论】:

    标签: ios objective-c uinavigationcontroller


    【解决方案1】:

    导航控制器从视图控制器获取标题,所以 这样就可以了

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

    【讨论】:

      【解决方案2】:

      这对我有用:

      self.navigationItem.title = @"YourTitle"; 
      

      希望对您有所帮助。

      【讨论】:

      • 添加导航项后,应使用此解决方案;相反,如果您没有添加任何导航项,您可以简单地使用:self.title。谢谢。
      【解决方案3】:
      self.title = @"My Title"
      

      这将更改导航栏标题以及引用此视图控制器的标题元素(例如标签栏等)

      self.navigationItem.title = @"My Title";
      

      这只会改变当前视图控制器导航栏的标题。

      【讨论】:

        【解决方案4】:

        你也可以替换标题视图:

        UIView* titleView = [[UIView alloc] init];
        [self.navigationItem setTitleView: titleView];
        

        【讨论】:

          【解决方案5】:

          如果你只更改导航栏标题,那么试试这个 关于 initwithnibname 方法

                  self.title=@"your title";
          

          但你想给标题的颜色或字体大小然后你想在它上面取一个 UIlable

                  UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
          
                  label.backgroundColor = [UIColor clearColor];
                  label.font = [UIFont boldSystemFontOfSize:20.0];
                  label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
                  label.textAlignment = UITextAlignmentCenter;
          
                  label.textColor = [UIColor blackColor];
                  self.navigationItem.titleView = label;
                  label.text =@"your title";
                  [label sizeToFit];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多