2013-11-15 35 views
9

私はアプリを作っており、ナビゲーションバーの上にロゴを入れてもらうように求められましたが、バーの外枠にも届き、私の見解の一部をカバーしました。これは正確に配置される方法です(白い丸はロゴです)。このアプリでは、複雑なビュー・コントローラの構造にUINavigationControllerのナビゲーションバーの上に画像を追加するには?

enter image description here

私はUINavigationControllerを使用したいが、それは少しより多くの問題のロゴを配置作ることができそうです。

どうすればよいですか? (明らかに、ナビゲーションバーのタイトルイメージとしてのロゴを奇妙な配置のために質問から外しているので)

私はそれをkeyWindowまたはnavigationController.viewのサブビューにすることを考えていました。最初のものが私のアプリをAppleに拒否させることはできますか?あなたがいないUINavigationControllerのサブクラスからUIViewControllerから画像を追加したい場合は

+0

同様の質問に、私は最近、ポストの答え - 私は私の質問で述べたように、これは、http://stackoverflow.com/questions/19878288/ios-custom-shape-navigation-bar – Kuba

答えて

13

あなたはこのような何かを行うことができます。

-(void)addImageOnTopOfTheNavigationBar { 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage"]]; 

    imageView.frame = CGRectMake(....); //set the proper frame here 
    [self.navigationController.view addSubview:imageView]; 

} 
2

ナビゲーションバーはtitleViewと呼ばれるものがあります。これはどのビューでも可能です。しかし、私はちょうどその上にイメージビューを置いた。

マインはinit関数内にあります。

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; 
+0

を見ておそらく良い考えではありません。主に、ナビゲーションバーの中央に収まるようにスケーリングされるため、バーの境界線の外側にストレッチする必要があるためです。 – johnyu

+0

ああフェア十分:)。 danypataのようにサブビューを追加する必要があります。自動サイズを設定してください – mashdup

1
UIImage*image = [UIImage imageNamed:@"logo"]; 

float targetHeight = self.navigationController.navigationBar.frame.size.height; 
float logoRatio = image.size.width/image.size.height; 
float targetWidth = targetHeight * logoRatio; 

UIImageView*logoView = [[UIImageView alloc] initWithImage:image]; 
// X or Y position can not be manipulated because autolayout handles positions. 
//[logoView setFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width - targetWidth)/2 , (self.navigationController.navigationBar.frame.size.height - targetHeight)/2 , targetWidth, targetHeight)]; 
[logoView setFrame:CGRectMake(0, 0, targetWidth, targetHeight)]; 
self.navigationItem.titleView = logoView; 

// How much you pull out the strings and struts, with autolayout, your image will fill the width on navigation bar. So setting only height and content mode is enough/ 
[logoView setContentMode:UIViewContentModeScaleAspectFit]; 

/* Autolayout constraints also can not be manipulated since navigation bar has immutable constraints 
self.navigationItem.titleView.translatesAutoresizingMaskIntoConstraints = false; 

NSDictionary*metricsArray = @{@"width":[NSNumber numberWithFloat:targetWidth],@"height":[NSNumber numberWithFloat:targetHeight],@"margin":[NSNumber numberWithFloat:20]}; 
NSDictionary*viewsArray = @{@"titleView":self.navigationItem.titleView}; 

[self.navigationItem.titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(>margin=)-H:[titleView(width)]-(>margin=)-|" options:NSLayoutFormatAlignAllCenterX metrics:metricsArray views:viewsArray]]; 
[self.navigationItem.titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleView(height)]" options:0 metrics:metricsArray views:viewsArray]]; 

NSLog(@"%f", self.navigationItem.titleView.width); 
*/ 

だから私たちは、実際に必要なのはあなたが簡単にIBからそれを行うことができます

UIImage*image = [UIImage imageNamed:@"logo"]; 
UIImageView*logoView = [[UIImageView alloc] initWithImage:image]; 
float targetHeight = self.navigationController.navigationBar.frame.size.height; 
[logoView setFrame:CGRectMake(0, 0, 0, targetHeight)]; 
[logoView setContentMode:UIViewContentModeScaleAspectFit]; 

self.navigationItem.titleView = logoView; 
2

です:

  1. (ないあなたのビュー階層で)自分のコントローラに画像を追加します。 enter image description here enter image description here
  2. UINavigationItemのタイトルビューをこの画像に接続します enter image description here
  3. Etvoilà! enter image description here
関連する問題