【问题标题】:Navigationbar title alignment issue导航栏标题对齐问题
【发布时间】:2015-01-10 06:02:54
【问题描述】:

我已将自定义视图设置为导航栏 titleView,当页面是第一个视图时,控制器标题正确显示在中心

但是当视图控制器从另一个视图控制器被推送时,标题会向右移动

代码:

-(void)setUpTwoLineNavigationTitle {
    CGFloat width = 0.95 * self.view.frame.size.width;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
    contentView.backgroundColor = [UIColor clearColor];

    CGRect titleRect = contentView.frame;
    titleRect.origin.y = 4;
    titleRect.size.height = 20;

    UILabel *titleView = [[UILabel alloc] initWithFrame:titleRect];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont systemFontOfSize:14.0];
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.textColor = [UIColor blackColor];
    titleView.text = @"";
    [contentView addSubview:titleView];

    CGRect subTitleRect = contentView.frame;
    subTitleRect.origin.y = 24;
    subTitleRect.size.height = subTitleRect.size.height - 24;
    //CGRect subtitleFrame = CGRectMake(0, 24, 220, 44-24);
    UILabel *subtitleView = [[UILabel alloc] initWithFrame:subTitleRect];
    subtitleView.backgroundColor = [UIColor clearColor];
    subtitleView.font = [UIFont systemFontOfSize:11.0];
    subtitleView.textAlignment = NSTextAlignmentCenter;
    subtitleView.textColor = [UIColor blackColor];
    subtitleView.text = @"";
    [contentView addSubview:subtitleView];

    contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    self.navigationItem.titleView = contentView;
}

谁能帮我纠正这个问题?

Github 链接:https://github.com/iamabhiee/NavigationTitleDemo

【问题讨论】:

  • 你的 contentView 太宽,所以它被后退按钮推到了右边。你需要它是视图宽度的 0.95 倍吗?在宽度的 0.6 倍处,它看起来会居中。
  • @rdelmar 谢谢,但如果我这样做,那么最初它看起来像左对齐,当子视图加载然后转移到中心
  • 我在您的测试应用程序中看不到这种情况。在任何情况下,您都需要缩小视图以使其居中。您可能还需要做其他事情才能摆脱您提到的问题。
  • 在这里查看我的答案:stackoverflow.com/a/33365965/4768700

标签: ios objective-c ios7 uinavigationbar


【解决方案1】:

这是一个解决方法,它可能有用,想法是使用 UILabel 作为navigationItemtitleView。这样您就不必手动计算和调整它的框架,它会由系统自动居中。不过,代码中的范围是硬编码的。

// here to create a UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;

// set different font for title and subtitle
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Title\nSubTitle"];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0,5)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11.0] range:NSMakeRange(6,8)];

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

【讨论】:

  • 工作就像一个魅力,谢谢:D
猜你喜欢
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2014-02-09
  • 2015-07-05
  • 2018-06-28
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多