无法为标签栏设置背景图片

发布于 2024-12-28 15:25:11 字数 1678 浏览 0 评论 0原文

我在标签栏上设置背景图像时遇到问题。我已经升级到 iOS5 和 Xcode 4.2,标签栏背景图像停止工作。

选项卡栏仅出现在我的 RootViewController 上。

我设法使用界面生成器并应用了背景颜色,这并不完全是我想要的,但现在会做。由于某种原因,这只适用于 iOS5。理想情况下,我想以编程方式完成它。我试图让以下代码正常工作:

RootViewController.h

@interface RootViewController : UIViewController {

    IBOutlet UITabBar *mainTabBar;
    IBOutlet UITabBarItem *settingsBarItem;
    IBOutlet UITabBarItem *infoBarItem;
    IBOutlet UITabBarItem *aboutBarItem;

}

@property (retain,nonatomic) UITabBar *mainTabBar;

@end

RootViewController.m

-(void)viewDidLoad {

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smallMenuBackground.png"]]; 
        if ([mainTabBar respondsToSelect:@selector(setBackgroundImage:)]){
            mainTabBar.backgroundImage = imageView;
        }
        else{
            CGRect frame = CGRectMake(0, 0, 480, 49);
            UIView *v = [[UIView alloc] initWithFrame:frame];
            UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
            UIColor *c = [[UIColor alloc] initWithPatternImage:i];
            v.backgroundColor = c;
            [c release];
            [mainTabBar insertSubview:v atIndex:0]; 
            [v release];
        }
}

这段代码对我不起作用,我一定做错了什么,所以任何帮助表示赞赏。

我还收到一些警告消息:

1)找不到实例方法“-respondToSelect:”(返回类型默认为“id”)

2)从“UIImageView *”分配给“UIImage *”的不兼容指针类型

编辑

整个代码太大,无法粘贴到此处,RootViewController.h 是 http://pastie.org/3249124 并且 RootViewController.m 是 http:// Pastie.org/3249137

I'm having trouble setting the background image on my tabbar. I've upgraded to iOS5 and Xcode 4.2, and the tabbar background image just stopped working.

The tabbar is only present on my RootViewController.

I managed to use the Interface Builder and applied a background colour which is not exactly what I wanted but will do for now. This only works for iOS5 for some reason. Ideally I would like to do it programmatically. I'm trying to get the following code working:

RootViewController.h

@interface RootViewController : UIViewController {

    IBOutlet UITabBar *mainTabBar;
    IBOutlet UITabBarItem *settingsBarItem;
    IBOutlet UITabBarItem *infoBarItem;
    IBOutlet UITabBarItem *aboutBarItem;

}

@property (retain,nonatomic) UITabBar *mainTabBar;

@end

RootViewController.m

-(void)viewDidLoad {

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smallMenuBackground.png"]]; 
        if ([mainTabBar respondsToSelect:@selector(setBackgroundImage:)]){
            mainTabBar.backgroundImage = imageView;
        }
        else{
            CGRect frame = CGRectMake(0, 0, 480, 49);
            UIView *v = [[UIView alloc] initWithFrame:frame];
            UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
            UIColor *c = [[UIColor alloc] initWithPatternImage:i];
            v.backgroundColor = c;
            [c release];
            [mainTabBar insertSubview:v atIndex:0]; 
            [v release];
        }
}

This code is just not working for me, I must be doing something wrong, so any help appreciated.

I'm getting a couple of warning messages aswell:

1) Instance method '-respondToSelect:' not found (return type defaults to 'id')

2) Incompatible pointer types assigning to 'UIImage *' from 'UIImageView *'

EDIT

The entire code is too large to paste here, the RootViewController.h is http://pastie.org/3249124 and the RootViewController.m is http://pastie.org/3249137

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

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

发布评论

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

评论(2

成熟的代价 2025-01-04 15:25:11

这行,不应该是

if ([mainTabBar respondsToSelector:@selector(setBackgroundImage:)])

respondsToSelector: 不是 respondsToSelect:

也,正如它所说,

mainTabBar.backgroundImage = imageView;

应该是

mainTabBar.backgroundImage = [UIImage imageNamed:@"smallMenuBackground.png"];

因为 backgroundImageUIImage 而你的 imageViewUIImageView

编辑

也,我发现你的 else 语句中的代码有点奇怪,尝试一下这是否有效。

else
{
    UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
    UIImageView* imageView = [UIImageView alloc] initWithImage:i];
    [mainTabBar insertSubview:imageView atIndex:0]; 
    [imageView release];
}

编辑2:

    if ([mainTabBar respondsToSelect:@selector(setTintColor:)])
    {
        UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
        UIColor* c = [[UIColor alloc] initWithPatternImage:i];
        mainTabBar.tintColor = c;
    }
    else
    {
        CGRect frame = CGRectMake(0, 0, 480, 49);
        UIView *v = [[UIView alloc] initWithFrame:frame];
        UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
        UIColor *c = [[UIColor alloc] initWithPatternImage:i];
        v.backgroundColor = c;
        [c release];
        [mainTabBar insertSubview:v atIndex:0]; 
        [v release];
    }

this line, shouldn't it be

if ([mainTabBar respondsToSelector:@selector(setBackgroundImage:)])

respondsToSelector: not respondsToSelect:

also, just as it said exactly,

mainTabBar.backgroundImage = imageView;

should be

mainTabBar.backgroundImage = [UIImage imageNamed:@"smallMenuBackground.png"];

because backgroundImage is UIImage while your imageView is UIImageView

Edit

also, I find that the code in your else statement is kind of weird, try if this is working.

else
{
    UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
    UIImageView* imageView = [UIImageView alloc] initWithImage:i];
    [mainTabBar insertSubview:imageView atIndex:0]; 
    [imageView release];
}

EDIT 2:

    if ([mainTabBar respondsToSelect:@selector(setTintColor:)])
    {
        UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
        UIColor* c = [[UIColor alloc] initWithPatternImage:i];
        mainTabBar.tintColor = c;
    }
    else
    {
        CGRect frame = CGRectMake(0, 0, 480, 49);
        UIView *v = [[UIView alloc] initWithFrame:frame];
        UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
        UIColor *c = [[UIColor alloc] initWithPatternImage:i];
        v.backgroundColor = c;
        [c release];
        [mainTabBar insertSubview:v atIndex:0]; 
        [v release];
    }
ζ澈沫 2025-01-04 15:25:11

你有一个小问题,你在 mainTabBar.backgroundImage 中传递 imageview 它已经磨损了,你将传递 UIImage ...请检查此代码

-(void)viewDidLoad {
            if ([mainTabBar respondsToSelect:@selector(setBackgroundImage:)]){
                mainTabBar.backgroundImage =  [UIImage imageNamed:@"smallMenuBackground.png"] ;
            }
            else{
                CGRect frame = CGRectMake(0, 0, 480, 49);
                UIView *v = [[UIView alloc] initWithFrame:frame];
                UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
                UIColor *c = [[UIColor alloc] initWithPatternImage:i];
                v.backgroundColor = c;
                [c release];
                [mainTabBar insertSubview:v atIndex:0]; 
                [v release];
            }
    }

you have minor problem you pass imageview in mainTabBar.backgroundImage it's worng , you will pass UIImage ... please check this code

-(void)viewDidLoad {
            if ([mainTabBar respondsToSelect:@selector(setBackgroundImage:)]){
                mainTabBar.backgroundImage =  [UIImage imageNamed:@"smallMenuBackground.png"] ;
            }
            else{
                CGRect frame = CGRectMake(0, 0, 480, 49);
                UIView *v = [[UIView alloc] initWithFrame:frame];
                UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
                UIColor *c = [[UIColor alloc] initWithPatternImage:i];
                v.backgroundColor = c;
                [c release];
                [mainTabBar insertSubview:v atIndex:0]; 
                [v release];
            }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文