gpt4 book ai didi

iphone - 为标签栏设置背景图像

转载 作者:行者123 更新时间:2023-12-03 18:33:29 26 4
gpt4 key购买 nike

我正在尝试以编程方式设置应用程序中选项卡栏的背景图像。我的代码如下:

RootViewController.h

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

RootViewController.m

-(void)viewDidLoad {

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

[super viewDidLoad];
}

这对我不起作用。

更新

2012 年 1 月 23 日更新

好的,我已经取得了一些进展。自从我升级到 Xcode 4.2 和 IOS5 后,这个问题就停止了。我设法使用 Interface Builder 中的选项将其恢复,但现在它仅适用于 IOS5。理想情况下,我希望以编程方式工作,但现在我会选择 IB 解决方案。

我似乎无法让它在任何以前的版本中工作。

注意:我的 TabBar 仅位于我的 RootViewController 上,这是我的应用程序的主屏幕。

理想情况下,如果我能让 Nithin 建议的代码正常工作,那就太好了:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];

if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
[self.tabBarController.tabBar insertSubview:imageView atIndex:1];
}
else {
//iOS 4.whatever and below
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}

[imageView release];

如有任何帮助,我们将不胜感激。

问候,斯蒂芬

最佳答案

您可以使用 UITabBarController 的自定义类并覆盖您的 tabBarController。您可以在那里设置所需的按钮及其带有图像的操作。

这就是您的自定义标签栏 Controller 类的样子:

//CustomTabBarController.h

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {
UIButton *settingsButton;
UIButton *infoButton;
UIButton *aboutUsButton;
}

@property (nonatomic, retain) UIButton *settingsButton;
@property (nonatomic, retain) UIButton *infoButton;
@property (nonatomic, retain) UIButton *aboutUsButton;

-(void) addCustomElements;
-(void) selectTab:(int)tabID;

@end

//CustomTabBarController.m

#import "CustomTabBarController.h"

@implementation CustomTabBarController

@synthesize settingsButton, infoButton, aboutUsButton;

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];


}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addCustomElements];
}

-(void)addCustomElements
{
// Background
UIImageView* bgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBarBackground.png"]] autorelease];
bgView.frame = CGRectMake(0, 420, 320, 60);
[self.view addSubview:bgView];

// Initialise our two images
UIImage *btnImage = [UIImage imageNamed:@"settings.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"settingsSelected.png"];

self.settingsButton = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
settingsButton.frame = CGRectMake(10, 426, 100, 54); // Set the frame (size and position) of the button)
[settingsButton setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted]; // Set the image for the selected state of the button
[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateDisabled];
[settingsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];
[settingsButton setTag:101]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[settingsButton setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially

// Now we repeat the process for the other buttons
btnImage = [UIImage imageNamed:@"info.png"];
btnImageSelected = [UIImage imageNamed:@"infoSelected.png"];
self.infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(110, 426, 100, 54);
[infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[infoButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[infoButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
[infoButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

[infoButton setTag:102];

btnImage = [UIImage imageNamed:@"aboutUs.png"];
btnImageSelected = [UIImage imageNamed:@"aboutUsSelected.png"];
self.aboutUsButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutUsButton.frame = CGRectMake(210, 426, 100, 54);
[aboutUsButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
[aboutUsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

[aboutUsButton setTag:103];

// Add my new buttons to the view
[self.view addSubview:settingsButton];
[self.view addSubview:infoButton];
[self.view addSubview:aboutUsButton];

// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
[settingsButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[infoButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aboutUsButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClicked:(id)sender
{
int tagNum = [sender tag];
[self selectTab:tagNum];
}

- (void)selectTab:(int)tabID
{
switch(tabID)
{
case 101:
[settingsButton setSelected:true];
[infoButton setSelected:false];
[aboutUsButton setSelected:false];
break;
case 102:
[settingsButton setSelected:false];
[infoButton setSelected:true];
[aboutUsButton setSelected:false];
break;
case 103:
[settingsButton setSelected:false];
[infoButton setSelected:false];
[aboutUsButton setSelected:true];
break;
}
self.selectedIndex = tabID;
}

- (void)dealloc {
[settingsButton release];
[infoButton release];
[aboutUsButton release];

[super dealloc];
}

@end

希望这对您有很大帮助。

关于iphone - 为标签栏设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8909379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com