2013-11-15 60 views
9

我提出一个应用程序,并要求把一个标志在导航栏上,也达到了吧边界之外,覆盖我的部分观点。这正是它应该放置的方式(白色圆圈就是标识)。如何在UINavigationController的导航栏上添加图像?

enter image description here

由于这个程序我想使用的UINavigationController,但似乎它可以使放置标志更多的问题有点复杂视图控制器结构。

这样做的好方法是什么? (因为显然把标志作为导航栏的标题图像是由于奇怪的位置不可能的问题)

我想或者使其成为keyWindow或navigationController.view的子视图。第一个会导致我的应用被Apple拒绝吗?

+0

我对类似的问题最近后的答案 - 看看http://stackoverflow.com/questions/19878288/ios-custom-shape-navigation-bar – Kuba

回答

13

如果你想从UIViewController不是从UINavigationController一个子类,添加图像,你可以做这样的事情:

-(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的。现在可以是任何视图。但我只是把imageview放在上面。

煤矿是在init函数。

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

正如我在我的问题说,这是可能不是一个好主意。主要是因为它会得到缩放到适合的导航栏的中间,而我需要它了吧境外延伸。 – johnyu

+0

啊不够公平:)。你需要添加像danypata说的子视图。确保你设置了autoresize,否则它不会停留在中间 – 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); 
*/ 

因此,所有我们真正需要的是

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

您可以从IB做到这一点很容易:

  1. 的图像添加到您的控制器(未在视图层次) enter image description here enter image description here
  2. 您UINavigationItem的标题视图连接到此图像 enter image description here
  3. 的Et瞧! enter image description here