如何在UINavigationController导航栏上添加图像?

时间:2013-11-15 14:30:58

标签: ios uiview uinavigationcontroller uikit

我正在制作一个应用程序,并被要求在导航栏上放置一个徽标,但也会到达栏的边框之外,覆盖我的部分视图。这就是它应该如何放置(白色圆圈是徽标)。

enter image description here

由于此应用程序中的视图控制器结构复杂,我想使用UINavigationController,但似乎它可能会使徽标更有问题。

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

我正在考虑将其作为keyWindow或navigationController.view的子视图。第一个可以导致我的应用被Apple拒绝吗?

4 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分:3)

您可以轻松地从IB进行操作:

  1. 将图像添加到控制器(不在视图层次结构中) enter image description here enter image description here
  2. 将您的UINavigationItem的标题视图连接到此图片 enter image description here
  3. Etvoilà! enter image description here

答案 2 :(得分:2)

导航栏中有一个名为titleView的东西。现在这可以是任何观点。但我只是在上面写了一个imageview。

我的是初始函数。

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

答案 3 :(得分: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;