无法为标签栏设置背景图片
我在标签栏上设置背景图像时遇到问题。我已经升级到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这行,不应该是
respondsToSelector:
不是respondsToSelect:
也,正如它所说,
应该是
因为
backgroundImage
是UIImage
而你的imageView
是UIImageView
编辑
也,我发现你的 else 语句中的代码有点奇怪,尝试一下这是否有效。
编辑2:
this line, shouldn't it be
respondsToSelector:
notrespondsToSelect:
also, just as it said exactly,
should be
because
backgroundImage
isUIImage
while yourimageView
isUIImageView
Edit
also, I find that the code in your else statement is kind of weird, try if this is working.
EDIT 2:
你有一个小问题,你在 mainTabBar.backgroundImage 中传递 imageview 它已经磨损了,你将传递 UIImage ...请检查此代码
you have minor problem you pass imageview in mainTabBar.backgroundImage it's worng , you will pass UIImage ... please check this code