【问题标题】:set the alignment of title of navigation bar设置导航栏标题的对齐方式
【发布时间】:2012-06-11 11:28:51
【问题描述】:

我正在尝试将导航栏的标题与我的应用程序的中心对齐,但标题似乎停留在右侧(请查找屏幕截图)。我正在使用下面的代码:

-(void)viewDidLoad {
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(goToHomePage:)];
    [self.navigationController.navigationItem.rightBarButtonItem setTarget:self];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}

viewWillAppear() 中如下

[self.navigationItem setTitle:offertitle];

也试过了

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 120, 40)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentLeft;
label.textColor =[UIColor whiteColor];
label.text=offertit;
self.navigationItem.titleView = label;

当我隐藏后退按钮时,标题显示居中对齐的标题。有什么方法可以设置后退按钮的外观属性吗?

image link here

谁能指导我哪里出错了。

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch


    【解决方案1】:

    您不必使用 UILabel。你可以直接使用这段代码:

    [self.navigationItem setTitle:@"Your Title"];
    

    你可以走了!此代码将自动将其自身放置在导航栏的中心。

    或者,如果您确实有 UINavigationController 的实例,请使用:

    [self setTitle:@"Your Title"];
    

    【讨论】:

    • 我有同样的问题,但它对我不起作用,我得到了相同的结果,如问题图像所示。
    【解决方案2】:

    需要在上一个viewcontroller的viewWillDisappear方法中设置self.title = ""。

    例如。 ViewControllerB 从 ViewControllerA 打开然后在 ViewControllerA 的 "viewWillDisappear" 方法中设置 self.title = ""

    【讨论】:

      【解决方案3】:
      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
      {
          self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
          if (self)
          {
              // this will appear as the title in the navigation bar
              UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
              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 yellowColor]; // change this color
              self.navigationItem.titleView = label;
              label.text = NSLocalizedString(@"PageThreeTitle", @"");
              [label sizeToFit];
          }
      
          return self;
      }
      

      【讨论】:

        【解决方案4】:

        只需在 ViewDidLoad 中调用它

        - (void)prepareNavigationTitle
        {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-50, 0.0, 240.0, 40.0)];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont boldSystemFontOfSize:14.0];
            label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
            label.textAlignment = UITextAlignmentLeft;
            label.textColor =[UIColor whiteColor];
            label.text=offertit;
            self.navigationItem.titleView = label;
        }
        

        【讨论】:

          【解决方案5】:

          如果您有导航控制器,则只需添加:self.title=@"Your Title";

          【讨论】:

            【解决方案6】:

            尝试使用 self.navigationItem.title = @"YourTitle";此代码会将标题放在导航栏的中心。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-01-10
              • 2014-02-09
              • 2015-07-05
              • 1970-01-01
              • 1970-01-01
              • 2011-09-03
              • 2011-01-17
              • 1970-01-01
              相关资源
              最近更新 更多