如何在UINavigationController的导航栏上方添加图片?

10

我正在制作一款应用程序,并被要求在导航栏上放置一个标志,但标志需要延伸到导航栏边框之外,遮挡部分视图。这是标志的确切放置方式(白色圆圈是标志)。

输入图像描述

由于这个应用程序中的复杂视图控制器结构,我想使用UINavigationController,但它似乎会使放置标志变得更加棘手。

有什么好方法可以解决这个问题吗?(由于奇怪的放置方式,将标志作为导航栏标题图像显然是不可行的)

我考虑过将其作为keyWindow或navigationController.view的子视图。第一个选项会导致我的应用被苹果拒绝吗?


我最近在类似的问题上发布了答案 - 请查看https://dev59.com/H3jZa4cB1Zd3GeqPkPMK - Jakub
4个回答

15

如果您想要从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];

}

3
导航栏有一个名为titleView的东西。它可以是任何视图,但我只把一个ImageView放在上面。
我的ImageView是在init函数中创建的。
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];

正如我在问题中所说,这可能不是一个好主意。主要是因为它将被缩放以适应导航栏的中间位置,而我需要它能够延伸到导航栏边界之外。 - johnyu
好的,明白了:). 你需要像danypata说的那样添加一个子视图。确保设置自动调整大小,否则它不会保持在中间。 - mashdup

3
你可以轻松地从IB中完成它:
  1. 将图像添加到您的控制器(不在您的视图层次结构中) 输入图片描述 输入图片描述
  2. 将您的UINavigationItem的标题视图连接到此图像 输入图片描述
  3. Et voilà! 输入图片描述

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接