【问题标题】:Navigation bar title alignment导航栏标题对齐
【发布时间】:2016-04-26 07:01:28
【问题描述】:

[[UIBarButtonItem appearance] 

    setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTitleTextAttributes:
         @{NSForegroundColorAttributeName:RED_COLOR ,NSFontAttributeName:[UIFont fontWithName:@"OpenSans-Semibold" size:16]}];
        self.navigationItem.title=@"ORDER DETAILS";

我正在使用上面的代码为视图控制器设置标题,但标题不正确。在某些视图控制器中,它按预期来了

【问题讨论】:

  • ,参考这个链接,它可以帮助你stackoverflow.com/questions/26903013/…
  • 您可以创建自定义标签并添加到导航栏标题视图,设置您想要的对齐方式。
  • @Elangovan 我需要没有自定义标签
  • @vijeesh 尝试为您的标题添加背景颜色可能是标题标签内容太大而无法容纳导航栏区域

标签: ios objective-c xcode uinavigationcontroller


【解决方案1】:
  // here to create a UILabel

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.numberOfLines = 1;
// set your custom font for title

    NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"ORDER DETAILS"];
    [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0,5)];

// set line spacing
    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrahStyle setLineSpacing:6];
    [paragrahStyle setAlignment:NSTextAlignmentCenter];
    [string addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [string length])];

    label.attributedText = string;
    [label sizeToFit];
    self.navigationItem.titleView = label;

复制自:Navigationbar title alignment issue

或者更好的办法是将标题设为 UiImage,您可以使用该图像作为导航栏标题。使用具有 2x 和 3x 版本的适当大小的 44x44 pt 图像。

UIImageView* titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Orderpng.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;

UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
imageView.frame = titleView.bounds;
[titleView addSubview:imageView];

self.navigationItem.titleView = titleView;

享受编码:)

【讨论】:

    【解决方案2】:

    对于寻找 Xamarin 解决方案的任何人:

    var label = new UILabel();
                label.Text = title;
                label.Font = UIFont.FromName(Strings.OpenSans_Bold, 30f);//This is a custom font. You will have to add it to your project first.
                label.TextAlignment = UITextAlignment.Left;
                label.SizeToFit();
                NavigationController.NavigationBar.TopItem.LeftBarButtonItem = new UIBarButtonItem(label);
    

    【讨论】:

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