iOS开发-设置tabBar的背景图片
2014-09-06 18:22
393 查看
设置tabBar的背景图片的原理是在tabBar上添加UIImageView,并且点击不同的Index则切换不同的图片。
(1)新建文件RootViewController,继承于UITabBarController,并且设置委托UITabBarControllerDelegate;
(2)在AppDelegate.m中didFinishLaunchingWithOptions下添加代码如下:
RootViewController *root = [[RootViewController alloc]initWithNibName:nil bundle:nil];
self.window.rootViewController = root;
(3)RootViewController.h中:
@interface RootViewController : UITabBarController<</span>UITabBarControllerDelegate>
{
UIImageView *selectImage;
}
- (void)loadRootViewController;
@end
(4)RootViewController.m中:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self loadRootViewController];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
selectImage = [[UIImageView alloc]init];
selectImage.frame = CGRectMake(0, -1, 320, 50);
[self.tabBar addSubview:selectImage];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
BOOL should = YES;
if(tabBarController.selectedViewController == viewController) {
should = NO;
}
return should;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController*)viewController
{
switch (tabBarController.selectedIndex) {
case 0:
selectImage.image = [UIImage imageNamed:@"tab01"];
break;
case 1:
selectImage.image = [UIImage imageNamed:@"tab02"];
break;
case 2:
selectImage.image = [UIImage imageNamed:@"tab03"];
break;
case 3:
selectImage.image = [UIImage imageNamed:@"tab04"];
default:
break;
}
}
-(void)loadRootViewController {
MusicKYFViewController *music = [[MusicKYFViewController alloc]init];
UINavigationController *navOne = [[UINavigationController alloc]initWithRootViewController:music];
tableKYFViewController *table = [[tableKYFViewController alloc]init];
UINavigationController *navTwo = [[UINavigationController alloc]initWithRootViewController:table];
TestOneViewController *testOne = [[TestOneViewController alloc]init];
UINavigationController *navThr = [[UINavigationController alloc]initWithRootViewController:testOne];
TestTwoViewController *testTwo = [[TestTwoViewController alloc]init];
UINavigationController *navFou = [[UINavigationController alloc]initWithRootViewController:testTwo];
self.viewControllers = [NSArray arrayWithObjects:navOne,navTwo,navThr,navFou, nil];
[self setSelectedIndex:1];
selectImage.image = [UIImage imageNamed:@"tab02"];
self.delegate = self;
}
效果如下图所示(图片尺寸最好为640*98):
(1)新建文件RootViewController,继承于UITabBarController,并且设置委托UITabBarControllerDelegate;
(2)在AppDelegate.m中didFinishLaunchingWithOptions下添加代码如下:
RootViewController *root = [[RootViewController alloc]initWithNibName:nil bundle:nil];
self.window.rootViewController = root;
(3)RootViewController.h中:
@interface RootViewController : UITabBarController<</span>UITabBarControllerDelegate>
{
UIImageView *selectImage;
}
- (void)loadRootViewController;
@end
(4)RootViewController.m中:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self loadRootViewController];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
selectImage = [[UIImageView alloc]init];
selectImage.frame = CGRectMake(0, -1, 320, 50);
[self.tabBar addSubview:selectImage];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
BOOL should = YES;
if(tabBarController.selectedViewController == viewController) {
should = NO;
}
return should;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController*)viewController
{
switch (tabBarController.selectedIndex) {
case 0:
selectImage.image = [UIImage imageNamed:@"tab01"];
break;
case 1:
selectImage.image = [UIImage imageNamed:@"tab02"];
break;
case 2:
selectImage.image = [UIImage imageNamed:@"tab03"];
break;
case 3:
selectImage.image = [UIImage imageNamed:@"tab04"];
default:
break;
}
}
-(void)loadRootViewController {
MusicKYFViewController *music = [[MusicKYFViewController alloc]init];
UINavigationController *navOne = [[UINavigationController alloc]initWithRootViewController:music];
tableKYFViewController *table = [[tableKYFViewController alloc]init];
UINavigationController *navTwo = [[UINavigationController alloc]initWithRootViewController:table];
TestOneViewController *testOne = [[TestOneViewController alloc]init];
UINavigationController *navThr = [[UINavigationController alloc]initWithRootViewController:testOne];
TestTwoViewController *testTwo = [[TestTwoViewController alloc]init];
UINavigationController *navFou = [[UINavigationController alloc]initWithRootViewController:testTwo];
self.viewControllers = [NSArray arrayWithObjects:navOne,navTwo,navThr,navFou, nil];
[self setSelectedIndex:1];
selectImage.image = [UIImage imageNamed:@"tab02"];
self.delegate = self;
}
效果如下图所示(图片尺寸最好为640*98):
相关文章推荐
- IOS开发教程--如何去除tabbar顶部线条及设置背景图片
- iOS - 如何去除tabbar顶部线条及设置背景图片
- ios 开发中,如何设置 uitabbar 里面 tabbaritem 不显示文字,只显示图片,图片垂直居中?
- IOS开发:关于设置UIView的背景为图片的方法以及问题
- iOS开发笔记--关于设置UIView的背景为图片的方法以及问题
- IOS开发:关于设置UIView的背景为图片的方法以及问题
- 【iOS开发】设置TabBarItem上文字跟图片一样的颜色
- iOS开发- UITabBarController的TabBar、TabBarItem背景颜色设置
- iOS开发笔记--关于设置UIView的背景为图片的方法以及问题
- iOS - 如何去除tabbar顶部线条及设置背景图片
- iOS开发笔记--将图片设置为主界面背景
- iOS开发之--如何修改TabBarItem的title的字体和颜色/BarButtonItem的title的字体大小和颜色/添加背景图片,并添加点击方法
- iOS开发笔记--将图片设置为主界面背景
- Objective-C ,ios,iphone开发基础:Category(分类,类别,范畴) ,UINavigationBar设置背景图片适配
- IOS开发:关于设置UIView的背景为图片的方法以及问题
- ios开发之改变UIAlertView背景图片
- iOS中设置视图的背景图片
- IOS 设置背景图片
- IOS 在IOS6中设置navigationBar背景图片 会有一条 黑色阴影 --- 解决方案
- 设置tabbar标签栏的背景图片 insertSubview: atIndex: