IOS5 SDK中UIToolbar上的自定义背景图片

发布于 2024-11-14 06:51:17 字数 275 浏览 3 评论 0原文

昨天下载了 IOS5 SDK,我用来将 UIToolbar 的背景设置为自定义图像的代码停止工作。如果我将目标设置为 IOS4.3 及以下版本,它仍然有效。

[self.bizToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar-iphone.png"]] autorelease] atIndex:0];

有人在 IOS 5 上遇到过这个问题吗?

Downloaded IOS5 SDK yesterday, and this code which I use to set my UIToolbar's background to a custom image stopped working. If I set the target to IOS4.3 and below it still works.

[self.bizToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar-iphone.png"]] autorelease] atIndex:0];

Anybody encountered this yet on IOS 5?

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

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

发布评论

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

评论(5

箜明 2024-11-21 06:51:17

您可以使用:[[UIToolBar外观] setBackgroundImage:toolBarIMG
forBarMetrics:UIBarMetricsDefault];
(iOS 5 中的新增功能)

You can use: [[UIToolBar appearance] setBackgroundImage:toolBarIMG
forBarMetrics:UIBarMetricsDefault];
(new in iOS 5)

枉心 2024-11-21 06:51:17

假设你链接了iOS5 beta SDK,你可以做这样的事情

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        //iOS 5 new UINavigationBar custom background
        [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
} 

来实现这一点,
看看这里 iOS 4.3 到 iOS 5.0 API 差异
并搜索“UINavigationBar.h”

或在此处仔细查看新方法签名
setBackgroundImage:forBarMetrics:

这里还有 UIBarMetrics 枚举类型

希望这有帮助。

Suppose you linked iOS5 beta SDK, you could do something like this

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        //iOS 5 new UINavigationBar custom background
        [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
} 

To realize this,
take a look at here iOS 4.3 to iOS 5.0 API Differences
and search for "UINavigationBar.h"

or take a close look at the new method signature here
setBackgroundImage:forBarMetrics:

Also here is the UIBarMetrics enum type

Hope this helps.

巷雨优美回忆 2024-11-21 06:51:17

这在我的工具栏上有效:

//toolBar background image set based on iOS version
    [[UIDevice currentDevice] systemVersion];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

        //iOS 5
        UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"];  

        if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
            [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
        }

    } else {

        //iOS 4
        [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0]; 

    }

This worked on my toolbar:

//toolBar background image set based on iOS version
    [[UIDevice currentDevice] systemVersion];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

        //iOS 5
        UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"];  

        if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
            [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
        }

    } else {

        //iOS 4
        [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0]; 

    }
苍景流年 2024-11-21 06:51:17

此方法没有记录,并且依赖于 UIToolbar 的特定子视图结构,该结构可以随版本的不同而更改。这正是 iOS5 版本可能发生的情况

PS 如果您检查更新的 UIToolBar class 参考您会找到另一种自定义 UIToolBar 的方法

This method is not documented and relies on specific subviews structure of UIToolbar which can be changed from version to version. So that exactly what probably happened with iOS5 release

P.S. If you check updated UIToolBar class reference you'll find another way to customize UIToolBar

成熟稳重的好男人 2024-11-21 06:51:17

这与 Simone 的答案相同,但适用于 iOS 5 和 iOS < 5. 这就是我在应用程序中使用的。您需要在应用程序初始化中的某个位置调用 [UINavigationBar setupIos5PlusNavBarImage] (applicationDidFinishLaunching: 是一个很好的候选者)。在 iOS 5+ 上,setupIos5PlusNavBarImage 将使用新的 UIAppearance 协议来设置背景,并且 drawRect 覆盖将被忽略。在 iOS 上< 5、setupIos5PlusNavBarImage 基本上是一个空操作,drawRect 将处理图像的绘制。

接口:

@interface UINavigationBar (CustomNavigationBar)

+ (void) setupIos5PlusNavBarImage;

- (void) drawRect: (CGRect) rect;

@end

实现:

@implementation UINavigationBar (CustomNavigationBar)

+ (void) setupIos5PlusNavBarImage
{
    if ([UINavigationBar respondsToSelector: @selector(appearance)])
    {
        [[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"menuBar.png"] forBarMetrics: UIBarMetricsDefault];
    }
}

- (void) drawRect: (CGRect) rect
{
    UIImage* img = [UIImage imageNamed: @"menuBar.png"];
    [img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

This is along the same line as Simone's answer, but works for iOS 5 and iOS < 5. This is what I'm using in app. You need to call [UINavigationBar setupIos5PlusNavBarImage] somewhere in your app initialization (applicationDidFinishLaunching: is a good candidate). On iOS 5+, setupIos5PlusNavBarImage will use the new UIAppearance protocol to set the background and the drawRect override will be ignored. On iOS < 5, setupIos5PlusNavBarImage will basically be a no-op and the drawRect will handle drawing the image.

Interface:

@interface UINavigationBar (CustomNavigationBar)

+ (void) setupIos5PlusNavBarImage;

- (void) drawRect: (CGRect) rect;

@end

Implementation:

@implementation UINavigationBar (CustomNavigationBar)

+ (void) setupIos5PlusNavBarImage
{
    if ([UINavigationBar respondsToSelector: @selector(appearance)])
    {
        [[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"menuBar.png"] forBarMetrics: UIBarMetricsDefault];
    }
}

- (void) drawRect: (CGRect) rect
{
    UIImage* img = [UIImage imageNamed: @"menuBar.png"];
    [img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

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