【问题标题】:How to add image on top of UINavigationController's navigation bar?如何在 UINavigationController 导航栏顶部添加图像?
【发布时间】:2013-11-28 23:35:17
【问题描述】:

我正在制作一个应用程序,并被要求在导航栏上方放置一个徽标,但也超出了导航栏的边界,覆盖了我的部分视图。这就是它应该放置的方式(白色圆圈是徽标)。

由于这个应用程序中的视图控制器结构复杂,我想使用 UINavigationController,但它似乎会使放置徽标的问题更加严重。

有什么好的方法可以做到这一点? (显然,由于位置怪异,将徽标作为导航栏的标题图像是不可能的)

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

【问题讨论】:

标签: ios uiview uinavigationcontroller uikit


【解决方案1】:

如果你想从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】:

    你可以从 IB 轻松做到:

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

    【讨论】:

      【解决方案3】:

      导航栏有一个叫做titleView的东西。现在这可以是任何视图。但我只是在上面放了一个imageview。

      我的是 init 函数。

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

      【讨论】:

      • 正如我在问题中所说,这可能不是一个好主意。主要是因为它会被缩放以适应导航栏的中间,而我需要它延伸到栏的边界之外。
      • 够公平的:)。你需要像 danypata 说的那样添加一个子视图。确保你设置了自动调整大小,否则它不会停留在中间
      【解决方案4】:
      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多