「IOS開発」UITabarControllerを使用してカスタマイズサンプルプログラム

コード下記:
1.CCTabBarController.h
#import <UIKit/UIKit.h>
@interface CCTabBarController : UITabBarController
{
@private
UIImageView *_tarbarImageView;
UIImageView *_selectedIndicatroView;
BOOL _isShow;
}
@property (strong, nonatomic) UIButton *selectedButton;
//TabBarControllerを初期化
//controllers: UIViewController
//normalNames: 通常の画像
//selectedNames: 画像を選択
//model : (0–非表示, 1–表示)
– (id)initWithControllers:(NSArray *)controllers tabbarNormalImageNames:(NSArray *)normalNames tabbarSelectedImageNames:(NSArray *)selectedName competence:(NSArray *)model;

@end

@interface CCTabBarController (TabBar)
//背景画像を設定
– (void)setCCTabBarBackgroundImage:(UIImage *)image;
//TabBarを表示
– (void)showCCTabBar;
//TabBarを非表示
– (void)hideCCTabBar;

@end

@interface CCTabBarController (TabBarIndicator)
//動画画像を設定
– (void)setCCSelectedIndicatorImage:(UIImage *)image;

@end

2.CCTabBarController.m
#import “CCTabBarController.h"
#import “UIButton+CCButton.h"

@interface CCTabBarController ()

– (void)tarbarChanged:(UIButton *)button;

@end

@implementation CCTabBarController
@synthesize selectedButton = _selectedButton;

– (id)initWithControllers:(NSArray *)controllers tabbarNormalImageNames:(NSArray *)normalNames tabbarSelectedImageNames:(NSArray *)selectedName competence:(NSArray *)model
{
self = [self init];
if (self) {

//パーミッションによってcontroller,normalImage、selectedImageを設定

__block NSMutableArray *tempNormalImages = [NSMutableArray arrayWithArray:normalNames];
__block NSMutableArray *tempSelectedImages = [NSMutableArray arrayWithArray:selectedName];
__block NSMutableArray *tempControllers = [NSMutableArray arrayWithArray:controllers];
[controllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([[model objectAtIndex:idx] isEqualToString:@"0″]) {
[tempControllers removeObject:obj];
[tempNormalImages removeObjectAtIndex:idx];
[tempSelectedImages removeObjectAtIndex:idx];
}
if (idx == normalNames.count – 1) {
*stop = YES;
}
}];

CGRect screenRect = [[UIScreen mainScreen] bounds];
_tarbarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, screenRect.size.height – 49, screenRect.size.width, 49)];
[_tarbarImageView setImage:[UIImage imageNamed:@"tabbar_bg.jpg"]];
_tarbarImageView.userInteractionEnabled = YES;

for (int i = 0; i < tempNormalImages.count; i++) {
UIButton *button = [[UIButton alloc] initWithNormalImage:[UIImage imageNamed:[tempNormalImages objectAtIndex:i]] andSlected:[UIImage imageNamed:[tempSelectedImages objectAtIndex:i]] frame:CGRectMake(i * 64, 0, 64, 49)];
button.tag = i;
[button addTarget:self action:@selector(tarbarChanged:) forControlEvents:UIControlEventTouchUpInside];
[_tarbarImageView addSubview:button];
if (i == 0) {
_selectedButton = button;
}
}
_selectedButton.selected = YES;
_selectedIndicatroView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 49)];
[_selectedIndicatroView setImage:[UIImage imageNamed:@"tarbar_indicator.png"]];
[_tarbarImageView addSubview:_selectedIndicatroView];
[self.view addSubview:_tarbarImageView];
_isShow = YES;
[self setViewControllers:tempControllers animated:YES];
}
return self;
}

#pragma mark – tarbar
– (void)setCCTabBarBackgroundImage:(UIImage *)image
{
[_tarbarImageView setImage:image];
}

– (void)showCCTabBar
{
if (!_isShow) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35f];
_tarbarImageView.frame = CGRectMake(0, _tarbarImageView.frame.origin.y, _tarbarImageView.frame.size.width, _tarbarImageView.frame.size.height);
[UIView commitAnimations];
_isShow = YES;
}
}

– (void)hideCCTabBar
{
if (_isShow) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35f];
_tarbarImageView.frame = CGRectMake(- _tarbarImageView.frame.size.width, _tarbarImageView.frame.origin.y, _tarbarImageView.frame.size.width, _tarbarImageView.frame.size.height);
[UIView commitAnimations];
_isShow = NO;
}
}

#pragma mark – tarbar indicator
– (void)setCCSelectedIndicatorImage:(UIImage *)image
{
[_selectedIndicatroView setImage:image];
}

#pragma mark – private
– (void)tarbarChanged:(UIButton *)button
{
if (self.selectedIndex != button.tag) {
_selectedButton.selected = NO;
_selectedButton = button;
[UIView animateWithDuration:.1f animations:^{
_selectedIndicatroView.frame = CGRectMake(_selectedButton.tag * 64, 0, 64, 49);
} completion:^(BOOL finished) {
self.selectedIndex = button.tag;
_selectedButton.selected = YES;
}];
}
}

@end
//UIViewControllerを追加,UINavigationControllerを使用する
NetworkUnitController *networkUnit = [[NetworkUnitController alloc] init];
UINavigationController *navNetworkUnit = [[UINavigationController alloc]initWithRootViewController:networkUnit];
if ([navNetworkUnit.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[navNetworkUnit.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
}

FireDisastorController *fireDisastor = [[FireDisastorController alloc] init];
UINavigationController *navFireDisastor = [[UINavigationController alloc] initWithRootViewController:fireDisastor];
if ([navFireDisastor.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[navFireDisastor.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
}

ModifyApplicationController *modifyApplication = [[ModifyApplicationController alloc] init];
UINavigationController *navModifyApplication = [[UINavigationController alloc] initWithRootViewController:modifyApplication];
if ([navModifyApplication.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[navModifyApplication.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
}

//オプション画像
NSArray *controllers = @[navNetworkUnit, navFireDisastor, navModifyApplication];
NSArray *normalImages = @[@"area_networkunit_normal.jpg", @"area_firedisastor_normal.jpg", @"area_modifyapplication_normal.png"];
NSArray *selectedImages = @[@"area_networkunit_selected.jpg", @"area_firedisastor_selected.jpg", @"area_modifyapplication_selected.png"];
//権限
NSArray *model = @[@"1″, @"1″, @"1″];
//初期化
CCTabBarController *tabBarController = [[CCTabBarController alloc] initWithControllers:controllers tabbarNormalImageNames:normalImages tabbarSelectedImageNames:selectedImages competence:model];
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
[self presentViewController:tabBarController animated:NO completion:^{

}];
}

IOS

Posted by arkgame