iPad 上 iOS 5 横向模式下的导航栏背景图像

发布于 2024-12-10 00:26:55 字数 2163 浏览 1 评论 0原文

我为此苦苦挣扎了一段时间,但找不到任何有相关问题的人。 我的问题是,在 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
  • header~ipad

编辑:屏幕截图导航栏上的问题:

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

Im struggling with this for a while and i cant find anyone with a related issue.
My problem is that the background image that gets loaded on for the landscape mode on the iPad its not the correct one (it loades the portrait version of the image).
On the iphone or iPod it work as its supposed to.

The code im using on my AppDelegate file is the following:

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]];
}

The problem is in this line?

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

My images names are the following:

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

Edit: Screenshots of the problem on the navigation bar:

Anyone with this issue?
Im open to ideas on how to solve this, tkz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

优雅的叶子 2024-12-17 00:26:55

只需使用此代码:

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

Just use this Code:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"ipad-menubar.png" ] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forBarMetrics:UIBarMetricsDefault];
中二柚 2024-12-17 00:26:55

附加〜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];
    }
}

,因此对您的问题没有好处,尽管这可能对其他读者有用。

Does appending ~iPad work for the portrait mode?

It isn't for me. Do you also have to specify ~iPhone to get it to work?

I got a separate image working for the iPad by doing

- (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];
    }
}

But for landscape mode it uses the portrait image and repeats the last pixel column to complete the missing portion, so no good for your problem although this may be of use for other readers.

脱离于你 2024-12-17 00:26:55

我无法管理适用于 iPad 的旋转外观方法。即使正确调整了导航栏框架的大小,图形也不会改变。最后,我挂钩了所有控制器的 viewWillAppear:animatedwillAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientationuration:(NSTimeInterval)duration 方法,以简单地调用 setBackgroundImage:< /code> 在 iPad 上使用正确的版本。丑陋,但有效。

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

I wasn't able to manage the appearance method working for the iPad with rotation. Even though the frame of the navigation bar is resized correctly, the graphic doesn't change. In the end I hooked the viewWillAppear:animated and willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration methods of all my controllers to simply call setBackgroundImage: with the correct version on the iPad. Ugly, but works.

It's possible this might be a bug worth reporting to Apple. The names of the defines themselves are also confusing, UIBarMetricsLandscapePhone doesn't make sense on iPads and your default app orientation could be landscape!

百善笑为先 2024-12-17 00:26:55

我认为你需要做这样的事情:

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)];

}

I think you need to do something like this:

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)];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文