簡體   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";

我正在使用上面的代碼為視圖控制器設置標題,但標題沒有正確顯示。 在某些視圖控制器中,它按預期到來

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

復制自: 導航欄標題對齊問題

或者更好的主意是將標題作為 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;

享受編碼:)

對於任何尋找 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