如何在中间对齐自定义 UINavigationBar 标题(UILabel)(iOS)

Posted

技术标签:

【中文标题】如何在中间对齐自定义 UINavigationBar 标题(UILabel)(iOS)【英文标题】:How to align custom UINavigationBar title (UILabel )in middle (iOS) 【发布时间】:2015-05-06 04:45:01 【问题描述】:

我正在使用以下代码在 UINavigationBar 中心对齐 2 个字符串。

例如:

         <Back     John Kelly Maria
                       23/F

我在 UINavigationbar 中需要这种中间对齐的文本,但现在得到的结果是所有文本都在 UINavigationBar 的右侧对齐。 像这样的

      <Back                  John Kelly Maria   
                                  23/F

请帮助..我。 这是我的代码

 UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)];

    UIFont * customFont = [UIFont fontWithName:@"Helvetica Neue" size:19]; //custom font
    NSString * text =title;
    UILabel *patientNameLabel;
    if([title length]>15)
        patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
    else
        patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];

    
    patientNameLabel.text = @"John Kelly Maria";
    patientNameLabel.font = customFont;
    patientNameLabel.numberOfLines = 0;
    patientNameLabel.textAlignment=NSTextAlignmentCenter;
    patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
    patientNameLabel.clipsToBounds = YES;
    patientNameLabel.backgroundColor = [UIColor clearColor];
    patientNameLabel.textColor = [UIColor blackColor];
    patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    NSString *subTitle =subTitleText;
    UIFont * customFont1 = [UIFont fontWithName:@"Helvetica Neue" size:14]; //custom font

    UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
    subTitleLabel.textAlignment = NSTextAlignmentCenter;

    subTitleLabel.text = @"23/F";
    subTitleLabel.font = customFont1;
     subTitleLabel.textAlignment=NSTextAlignmentCenter;
    subTitleLabel.numberOfLines = 1;

    subTitleLabel.adjustsFontSizeToFitWidth = YES;
    subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
    subTitleLabel.clipsToBounds = YES;
    subTitleLabel.backgroundColor = [UIColor clearColor];
    subTitleLabel.textColor = [UIColor blackColor];
    [titleView addSubview:patientNameLabel];
    [titleView addSubview:subTitleLabel];

【问题讨论】:

你能把你得到的截图放上去吗,它对回答你的问题更有帮助。 好的..我会添加截图链接 【参考方案1】:

在 addSubview 行之前添加以下两行..

patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 0);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 25);

添加下面的代码并查看

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];

UIFont * customFont = [UIFont fontWithName:@"Helvetica Neue" size:19]; //custom font
NSString * text =@"Nilesh Patel";
UILabel *patientNameLabel;
if([text length]>15)
    patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
else
    patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];


patientNameLabel.text = @"Nilesh Patel";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =@"ios Developer";
UIFont * customFont1 = [UIFont fontWithName:@"Helvetica Neue" size:14]; //custom font

UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;

subTitleLabel.text = @"28/M";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;

subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];

patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 30);


[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
[self.navigationController.navigationBar addSubview:titleView];

注意:您已将静态框架设置为 titleView,因此如果您在不同的设备上看到您的应用程序,您将无法在准确的中心看到文本。为避免这种情况,请根据设备/视图宽度动态设置 titleView 框架。

【讨论】:

它移到右上角(parentNameLabel 与状态栏冲突)角落...没有按我想要的方式工作 将 10 像素添加到 Y 位置.. Like.. patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10); subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 35); 右角是什么意思?我必须在您的原始帖子中引用哪张图片? 做一件事.. 将标题视图居中。比如titleView.center = CGPointMake(self.view.frame.size.width/2, 10); 我上传截图的链接【参考方案2】:

试试这样的框架设置

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,20,screenWidth,50)];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,screenWidth,20)];
UILabel *patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,screenWidth,30)];

【讨论】:

以上是关于如何在中间对齐自定义 UINavigationBar 标题(UILabel)(iOS)的主要内容,如果未能解决你的问题,请参考以下文章

居中对齐标题菜单:Wordpress(Avada 主题)自定义 CSS

IOS - 垂直对齐导航栏中的后退按钮

如何在 Android 自定义视图中拖动和对齐项目?

如何在 android 应用程序中正确对齐自定义字体

如何在自定义按钮中对齐中心文本?

如何在 AppKit 上的自定义 SwiftUI 表单中右对齐项目标签?