【问题标题】:navigationBar Background Image on iPad with iOS 5 in landscape mode横屏模式下 iOS 5 iPad 上的导航栏背景图像
【发布时间】:2011-10-13 03:45:08
【问题描述】:

我为此苦苦挣扎了一段时间,但找不到任何有相关问题的人。 我的问题是在 iPad 上为横向模式加载的背景图像不是正确的(它加载图像的纵向版本)。 在 iphone 或 iPod 上它可以正常工作。

我在 AppDelegate 文件中使用的代码如下:

if ( [[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0 ) {
    // Create resizable images
    UIImage *gradientImageP = [[UIImage imageNamed:@"header"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    UIImage *gradientImageL = [[UIImage imageNamed:@"header-Landscape"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UINavigationBar appearance] setBackgroundImage:gradientImageP
                                       forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:gradientImageL
                                       forBarMetrics:UIBarMetricsLandscapePhone];
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
    [[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:0 green: 0 blue:0  alpha:1]];
}

问题出在这一行?

[[UINavigationBar appearance] setBackgroundImage:gradientImageL
                                   forBarMetrics:UIBarMetricsLandscapePhone];

我的图片名称如下:

  • header-Landscape~iphone.png
  • header-Landscape@2x~iphone.png
  • header-Landscape~ipad.png
  • header~iphone.png
  • header@2x~iphone.png
  • 标题~ipad

编辑:导航栏问题截图:

有人遇到这个问题吗? 我对如何解决这个问题持开放态度,tkz

【问题讨论】:

  • 您是否希望将 ~ipad 加载到 iPad 上?我以前没见过这个大会。你有一些自动为你做的代码吗?
  • 是的,我希望将带有 ~ipad 的文件名加载到该设备上。我在应用程序的其他图像上使用这种名称并且它们正确加载。无论如何,我为所有文件尝试了各种类型的文件名,但是对于导航栏,这个问题仍然存在:\

标签: objective-c ipad uinavigationbar landscape ios5


【解决方案1】:

只需使用此代码:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"ipad-menubar.png" ] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forBarMetrics:UIBarMetricsDefault];

【讨论】:

    【解决方案2】:

    附加 ~iPad 是否适用于纵向模式?

    它不适合我。您是否还必须指定 ~iPhone 才能让它工作?

    通过这样做,我得到了一张适用于 iPad 的单独图像

    - (void)customizeAppearance {
    
        if ([[UINavigationBar class]respondsToSelector:@selector(appearance)]) {
    
            // Create resizable images for iphone
            UIImage *navbarImage44 = [[UIImage imageNamed:@"navbar_bg_portrait"] 
                                      resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
            UIImage *navbarImage32 = [[UIImage imageNamed:@"navbar_bg_landscape"] 
                                      resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
            // Overide for iPad
            // In theory navbar_bg_landscape~iPad should work
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                //use iPad specific image extending last pixel for landscape mode
                navbarImage44 = [[UIImage imageNamed:@"navbar_bg_portrait~iPad"] 
                                          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 767, 0, 0)];
           }
    
            // Set the background image for *all* UINavigationBars
            [[UINavigationBar appearance] setBackgroundImage:navbarImage44 
                                               forBarMetrics:UIBarMetricsDefault];
            // Never called on iPad, used for landscape on iPhone
            [[UINavigationBar appearance] setBackgroundImage:navbarImage32 
                                               forBarMetrics:UIBarMetricsLandscapePhone];
        }
    }
    

    但对于横向模式,它使用纵向图像并重复最后一个像素列来完成缺失的部分,因此对您的问题没有好处,尽管这可能对其他读者有用。

    【讨论】:

    • Appendng ~iphone 和 ~ipad 在 iphone 版本上效果很好,但在 ipad 上不行。
    • 我已经测试了你的代码,如果条件适用于 ipad 并且它不适用于纵向版本,截图如下:img577.imageshack.us/img577/3118/semnomeq.png 正如我所说,它可能是模拟器问题?
    【解决方案3】:

    我无法管理适用于 iPad 旋转的外观方法。即使导航栏的框架大小调整正确,图形也不会改变。最后,我连接了我所有控制器的viewWillAppear:animatedwillAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 方法,只需在iPad 上使用正确版本调用setBackgroundImage:。丑陋,但有效。

    这可能是一个值得向 Apple 报告的错误。定义本身的名称也令人困惑,UIBarMetricsLandscapePhone 在 iPad 上没有意义,您的默认应用方向可能是横向!

    【讨论】:

      【解决方案4】:

      我认为你需要这样做:

      UIImage *gradientImageP;
      UIImage *gradientImageL;
      
      if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
          //use iPhone specific images
          gradientImageP = [[UIImage imageNamed:@"header~iphone.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
          gradientImageL = [[UIImage imageNamed:@"header-Landscape~iphone.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
      } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
          //use iPad specific images
          gradientImageP = [[UIImage imageNamed:@"header~Landscape~ipad.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
      
      }
      

      【讨论】:

      • 已经尝试使用 (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad),同样的问题:\
      • 我被教导认为当某些东西不起作用时,可能是因为我的代码有问题,但在这种情况下,我认为问题在于这个来自 iOS 5 的新类是“新"
      • 我只是想到了一些东西......你不需要 iPad 的“横向”图像......因为 UINavigationBar 在 iPhone 上的横向和纵向中的高度相同。跨度>
      • 我认为您需要将纵向图像设置为您的 ipad 横向图像以用于 ipad 成语。
      • 该实现的问题是,如果我在肖像模式下使用图像标题~Landscape~ipad.png,则应用程序的标题不会停留在屏幕中间。如果我缩小图像,它看起来很难看。我将使用我发布的“错误”代码启动对我的应用程序的更新,也许它将由 ios 5.0.1 解决
      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多