22

UIViewController の viewDidLoad で使用されるこのコード スニペットがあります。エラーはありません。画像が存在します。背景は取得できますが、画像は取得できません。画像は一種のロゴです。

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {

    /* Background of navigationBar. */
    UIImage * navigationBarImage = [UIImage imageNamed:@"01_navbar_portrait.png"];
    [self.navigationController.navigationBar setBackgroundImage:navigationBarImage forBarMetrics:UIBarMetricsDefault];

    /* Image in navigationBar */
    UIImage * logoInNavigationBar = [UIImage imageNamed:@"01_logo.png"];
    UIImageView * logoView = [[UIImageView alloc] init];
    [logoView setImage:logoInNavigationBar];
    self.navigationController.navigationItem.titleView = logoView;

}
4

7 に答える 7

54

は、ナビゲーションスタックの最上位のView ControllerUINavigationControllerのプロパティを確認することにより、ナビゲーションバーを管理します。navigationItemしたがって、ビューをロゴに変更するには、ロゴを使用するビューコントローラー(つまり、ルートビューコントローラーまたはスタックにプッシュされる別のコントローラー)でこれを設定する必要があります。

viewDidLoadビューコントローラで次のようなことを行います。

UIImage* logoImage = [UIImage imageNamed:@"logo.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logoImage];

あなたの場合、間違ったナビゲーション項目を設定しています:

// Oops...
self.navigationController.navigationItem.titleView = logoView;
// Should be this:
self.navigationItem.titleView = logoView;
于 2013-03-04T11:16:02.887 に答える
7

迅速:

var logoImage:UIImage = UIImage(named: "logo_text")!
self.navigationItem.titleView = UIImageView(image: logoImage)
于 2014-12-10T10:26:13.797 に答える
2

おそらくあなたが意図したものではありませんが、ナビゲーションバーの背景画像を中央に配置する方法を探しているときにこのページに出くわしました。

別の答えから少し盗むと、画像を前景と背景に分割し、背景を引き伸ばして前景を中央に配置する新しい画像を作成し、それをナビゲーションバーの背景画像として設定できます。イメージのビルドは次のように機能します。

// build an image by stretching the bg, then merging it with the fg
CGSize barSize = self.navController.navigationBar.frame.size;
UIImage *fg = [UIImage imageNamed:@"some_fg"];
UIImage *bg = [[UIImage imageNamed:@"some_bg"]
               resizableImageWithCapInsets:UIEdgeInsetsMake(0.f,1.f,0.f,1.f)];
UIGraphicsBeginImageContextWithOptions(barSize, NO, 0.0);
[bg drawInRect:CGRectMake(0, 0, barSize.width, barSize.height)];
[fg drawInRect:CGRectMake((barSize.width-fg.size.width)/2.f,
                          0,
                          fg.size.width,
                          fg.size.height)];
// grab the merged images
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2016-02-22T11:41:58.627 に答える
1

フレームを指定するだけです

logoView.frame = CGRectMake(initialize frame here);

次に、以下を使用します

[self.navigationItem setTitleView:logoView];
于 2013-03-04T11:15:39.810 に答える