2016-04-26 12 views
1

enter image description hereナビゲーションバーのタイトルアライメント

[[UIBarButtonItem appearance] 

    setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; 
     [self.navigationController.navigationBar setTitleTextAttributes: 
     @{NSForegroundColorAttributeName:RED_COLOR ,NSFontAttributeName:[UIFont fontWithName:@"OpenSans-Semibold" size:16]}]; 
     [email protected]"ORDER DETAILS"; 

私は、ビューコントローラのタイトルを設定するには、上記のコードを使用していますが、タイトルが適切に来ていません。コピー

+0

、このリンクを参照してください、それはhttp://stackoverflow.com/questions/26903013/あなたを助けるかもしれませんnavigationbar-title-alignment-issue –

+0

カスタムラベルを作成し、ナビゲーションバーのタイトルビューに追加して、必要な配置を設定することができます。 – Elangovan

+0

@エラゴヴァン私はカスタムラベルなしで必要があります – vijeesh

答えて

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

予想通り、いくつかのビューコントローラでは、それが来ているから:Navigationbar title alignment issue

または他のより良いアイデアをUiImageとしてタイトルを作られて、uはナビゲーションバーのタイトルとしてそのイメージを使用することができます。 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ソリューションを探している人のために:)

0

コーディング楽しむ:

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); 
関連する問題