如何在特定屏幕上隐藏自定义标签栏控制器?

时间:2015-08-17 11:33:32

标签: xcode uitabbarcontroller tabbar

我正在使用链接中的代码... Which is the best approach for creating custom tab bar controller?

现在我需要在页面之间隐藏tabbar。在下一页上它会像往常一样出现。如果有人可以帮我提供一些可用的链接或代码

,该如何做到这一点
#import "MyTabBarController.h"

@interface MyTabBarController ()

@property (strong, nonatomic) UIButton *btn1;
@property (strong, nonatomic) UIButton *btn2;
@property (strong, nonatomic) UIButton *btn3;
@property (strong, nonatomic) UIButton *btn4;
@property (weak, nonatomic) UIButton *lastSender;
@property (strong, nonatomic) UIView *tabbarView;

@end



@implementation MyTabBarController

-(id)init
{
    if (self=[super init]) {
        [self setUP];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super initWithCoder:aDecoder]) {
        [self setUP];
    }
    return self;
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self setUP];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.



    _tabbarView = [[[NSBundle mainBundle] loadNibNamed:@"MyTabBarController" owner:nil options:nil] lastObject];

    _tabbarView.frame = CGRectMake(0.0,
                                   [[UIScreen mainScreen] bounds].size.height-70,
                                   [[UIScreen mainScreen] bounds].size.width,70); // make it overlay your actual tab bar
    [self.view addSubview:_tabbarView];

    _btn1 = (UIButton *)[_tabbarView viewWithTag:1];
    [_btn1 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];

    _btn2 = (UIButton *)[_tabbarView viewWithTag:2];
    [_btn2 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];

    _btn3 = (UIButton *)[_tabbarView viewWithTag:3];
    [_btn3 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];

    _btn4 = (UIButton *)[_tabbarView viewWithTag:4];
    [_btn4 addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];

    _lastSender = _btn4;
    [self setSelectedViewController:self.viewControllers[3]];
}

-(void)processBtn:(id)sender
{
    _lastSender =(UIButton*) sender;
    [self setSelectedViewController:[self.viewControllers objectAtIndex:_lastSender.tag - 1]];
}

- (void)setSelectedViewController:(UIViewController *)selectedViewController
{

    if ([self.viewControllers indexOfObject:selectedViewController] == 0)
    {
        if (self.selectedViewController == selectedViewController)
        {
            [(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:YES]; // pop to root if tapped the same controller twice
        }
        [super setSelectedViewController:selectedViewController];
    }else if ([self.viewControllers indexOfObject:selectedViewController] == 1)
    {
        if (self.selectedViewController == selectedViewController)
        {
            [(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:YES]; // pop to root if tapped the same controller twice
        }
        [super setSelectedViewController:selectedViewController];
    }else if ([self.viewControllers indexOfObject:selectedViewController] == 2)
    {
        if (self.selectedViewController == selectedViewController)
        {
            [(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:YES]; // pop to root if tapped the same controller twice
        }
        [super setSelectedViewController:selectedViewController];
    }else if ([self.viewControllers indexOfObject:selectedViewController] == 3)
    {
        if (self.selectedViewController == selectedViewController)
        {
            [(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:YES]; // pop to root if tapped the same controller twice
        }
        [super setSelectedViewController:selectedViewController];
    }


    NSLog(@"%@",[_tabbarView subviews]);
    for (UIButton *btn in [_tabbarView subviews])
    {
        if ([btn isKindOfClass:[UIButton class]])
        {
            if (btn == _lastSender)
            {
                NSString *strImageName=@"";
                switch (btn.tag)
                {
                    case 1:
                        strImageName=@"settings_icon_hover.png";
                        break;
                    case 2:
                        strImageName=@"garage_icon_hover.png";
                        break;
                    case 3:
                        strImageName=@"help_icon_hover.png";
                        break;
                    case 4:
                        strImageName=@"new_test_hover.png";
                        break;

                    default:
                        break;
                }

                btn.selected = YES;
                UIImageView *imgSelected=(UIImageView*)[_tabbarView viewWithTag:btn.tag*10];
                [imgSelected setImage:[UIImage imageNamed:strImageName]];
                UILabel *lblSelected=(UILabel*)[_tabbarView viewWithTag:btn.tag*100];
                [lblSelected setTextColor:[UIColor colorWithRed:82.0f/255.0f green:160.0f/255.0f blue:205.0f/255.0f alpha:1.0f]];
            }
            else
            {
                btn.selected = NO;
                NSString *strImageName=@"";
                switch (btn.tag)
                {
                    case 1:
                        strImageName=@"settings_icon.png";
                        break;
                    case 2:
                        strImageName=@"garage_icon.png";
                        break;
                    case 3:
                        strImageName=@"help_icon.png";
                        break;
                    case 4:
                        strImageName=@"new_test.png";
                        break;

                    default:
                        break;
                }
                UIImageView *imgNonSelected=(UIImageView*)[_tabbarView viewWithTag:btn.tag*10];
                [imgNonSelected setImage:[UIImage imageNamed:strImageName]];
                UILabel *lblSelected=(UILabel*)[_tabbarView viewWithTag:btn.tag*100];
                [lblSelected setTextColor:[UIColor grayColor]];
            }

        }
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUP
{
    NSMutableArray *viewControllers = [NSMutableArray array];

    UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];

    UINavigationController *navControllerSettings=[storyBoard instantiateViewControllerWithIdentifier:@"settings"];
    [viewControllers addObject:navControllerSettings];

    UINavigationController *navControllerGarage=[storyBoard instantiateViewControllerWithIdentifier:@"garage"];
    [viewControllers addObject:navControllerGarage];

    UINavigationController *navControllerHelp=[storyBoard instantiateViewControllerWithIdentifier:@"help"];
    [viewControllers addObject:navControllerHelp];

    UINavigationController *navControllerNewTest=[storyBoard instantiateViewControllerWithIdentifier:@"newtest"];
    [viewControllers addObject:navControllerNewTest];

    self.viewControllers = viewControllers;
}
@end

0 个答案:

没有答案