简体   繁体   English

导航栏标题对齐

[英]Navigation bar title alignment

在此处输入图片说明

[[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";

I am using the above code to set title for view controller, but the title is not coming properly.我正在使用上面的代码为视图控制器设置标题,但标题没有正确显示。 In some view controller it is coming as expected在某些视图控制器中,它按预期到来

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

copied from : Navigationbar title alignment issue复制自: 导航栏标题对齐问题

or else the better idea is made the title as an UiImage and u can use that image as Navigation bar title.或者更好的主意是将标题作为 UiImage 并且您可以使用该图像作为导航栏标题。 Use a properly sized 44x44 pt image with 2x and 3x versions.使用具有 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;

enjoy coding :)享受编码:)

For anyone looking for a Xamarin Solution:对于任何寻找 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM