导航栏标题对齐问题

阿比舍克

我将自定义视图设置为导航栏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

加布勒

这是一个解决方法,可能有用,其想法是使用UILabel作为titleViewnavigationItem这样,您无需手动计算和调整其框架,它将自动在系统中居中。但是,代码中的范围是硬编码的。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

导航栏标题视图对齐

来自分类Dev

将导航栏与标题图像对齐

来自分类Dev

Twitter Bootstrap 3.0.3导航栏对齐导航栏标题

来自分类Dev

Twitter Bootstrap 3.0.3导航栏对齐导航栏标题

来自分类Dev

底部导航栏的图像/链接对齐问题

来自分类Dev

某些视图的导航栏大标题问题

来自分类Dev

如何在导航栏标题内对齐搜索表单?

来自分类Dev

当标题导航栏很大时,将文本视图与顶部对齐

来自分类Dev

将导航与标题中的徽标对齐时出现问题

来自分类Dev

小屏幕尺寸时的引导导航栏文本对齐问题

来自分类Dev

第一个导航栏对齐问题

来自分类Dev

Wordpress网站中的徽标和导航栏对齐问题

来自分类Dev

对齐导航栏

来自分类Dev

Angular 5 路由 - 标题和导航栏路由问题

来自分类Dev

多行导航栏标题

来自分类Dev

SwiftUI导航栏标题

来自分类Dev

导航栏标题更改

来自分类Dev

干扰导航栏的标题

来自分类Dev

覆盖导航栏标题

来自分类Dev

导航抽屉对齐问题

来自分类Dev

bootstrap 导航对齐问题

来自分类Dev

引导搜索框和按钮对齐到标题文本的右侧(不在导航栏中)

来自分类Dev

无法获得理由内容和对齐项目同时在我的标题和导航栏上工作

来自分类Dev

mincss右对齐导航栏

来自分类Dev

在div中对齐导航栏

来自分类Dev

如何使导航栏与中心对齐

来自分类Dev

导航栏不会向左对齐

来自分类Dev

在导航栏中对齐项目

来自分类Dev

mincss右对齐导航栏