我在标签栏上设置背景图片时遇到问题。我已升级到iOS5和Xcode 4.2,并且tabbar背景图像停止工作。
标签栏仅出现在我的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
答案 0 :(得分:2)
这一行,不应该是
if ([mainTabBar respondsToSelector:@selector(setBackgroundImage:)])
respondsToSelector:
不是respondsToSelect:
同样,正如它所说的那样,
mainTabBar.backgroundImage = imageView;
应该是
mainTabBar.backgroundImage = [UIImage imageNamed:@"smallMenuBackground.png"];
因为backgroundImage
为UIImage
而imageView
为UIImageView
编辑
另外,我发现你的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];
}
答案 1 :(得分:1)
你有一个小问题,你在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];
}
}