您的位置:首页 > 移动开发 > IOS开发

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):
  

            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息