开发者

Buttons won't hide in custom TabBar

开发者 https://www.devze.com 2023-03-10 19:14 出处:网络
using this tutorial I set up a custom TabBar. Unfortunately the tutorial won\'t describe how to hide the custom TabBar in views you don\'t wanna display it.

using this tutorial I set up a custom TabBar. Unfortunately the tutorial won't describe how to hide the custom TabBar in views you don't wanna display it.

In my customTabBar.h I defined

- (void) hideAlsoCustomTabBar;
- (void) showCustomTabBarAgain;

which are implemented as

- (void) hideAlsoCustomTabBar {

    btn1.hidden = YES;
    btn2.hidden = YES;
    btn3.hidden = YES;  
    btn4.hidden = YES;
}

- (void) showCustomTabBarAgain {

    btn1.hidden = NO;
    btn2.hidden = NO;
    btn3.hidden = NO;
    btn4.hidden = NO;
}

Calling those inside CustomTabBar.m's viewDidAppear works fine and does exactly what I expect them to do. If I try to call those methods from the ViewController where I need to hide the TabBar like this

[customTabs hideAlsoCustomTabBar];

inside the initWithNibName OR viewDidLoad OR viewWillAppear, nothing will happen. I checked with NSLog, the method gets called, but when I read out the BOOL for any buttons .hidden attribute, it returns 0, when it should be 1 (for hidden==YES). I don't know what's wrong with my setup. Is it possible these button's attributes are private by default as CustomTabBar inherits from UITabBarController and I can't set them? Or did I make a mistake in the call?

Thanks! EDIT The TabBar implementation as it is described in the tutorial

import "CustomTabBar.h"
@implementation CustomTabBar

@synthesize btn1,btn2,btn3,btn4;

- (void) viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideExistingTabBar];
[self addCustomElements];
}

- (void) hideExistingTabBar {
for (UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[UITabBar class]]) {
        view.hidden = YES;
        break;
    }
}
开发者_开发知识库}

- (void) addCustomElements {
//Initialise the two images for btnEinleitung, not selected and selected
UIImage *btnImage = [UIImage imageNamed:@"btnEinl.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"btnEinl_s.png"];

self.btnEinleitung = [UIButton buttonWithType:UIButtonTypeCustom]; 
btnEinleitung.frame = CGRectMake(0, 430, 86, 50);
[btnEinleitung setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnEinleitung setBackgroundImage:btnImageSelected  forState:UIControlStateSelected];   
[btnEinleitung setTag:0];
[btnEinleitung setSelected:true];

//set other buttons
btnImage = [UIImage imageNamed:@"btnUbg.png"];
btnImageSelected = [UIImage imageNamed:@"btnUbg_s.png"];
self.btnUebungen = [UIButton buttonWithType:UIButtonTypeCustom];
btnUebungen.frame = CGRectMake(86, 430, 80, 50);
[btnUebungen setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnUebungen setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnUebungen setTag:1];

   /* do the same for btn3 and btn4*/


//add custom buttons to view
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];

//setup event handlers so the buttonClicked method will respond to the touch up inside event
[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

}

//set tab to active according to the passed tag number 
- (void) selectTab:(int)tabID {
switch (tabID) {
    case 0:
        [btnEinleitung setSelected:TRUE];
        [btnUebungen setSelected:FALSE];
        [btnTipps setSelected:FALSE];
        [btnBrauchtum setSelected:FALSE];

        btnEinleitung.userInteractionEnabled = NO;
        btnUebungen.userInteractionEnabled = YES;
        btnTipps.userInteractionEnabled = YES;
        btnBrauchtum.userInteractionEnabled = YES;
        break;
    case 1:
        [btnEinleitung setSelected:FALSE];
        [btnUebungen setSelected:TRUE];
        [btnTipps setSelected:FALSE];
        [btnBrauchtum setSelected:FALSE];

        btnEinleitung.userInteractionEnabled = YES;
        btnUebungen.userInteractionEnabled = NO;
        btnTipps.userInteractionEnabled = YES;
        btnBrauchtum.userInteractionEnabled = YES;
        break;
// and so on for 2 and 3
    }
self.selectedIndex = tabID;
}

//get the tag of the sender/pressed button, call the function selectTab
- (void) buttonClicked:(id)sender {
int tagNum = [sender tag];
[self selectTab:tagNum];
}

EDIT As described below, I have 4 Tabs in a Tabbar which was generated with IB, added Navigation Controller with their ViewControllers, made Outlets for those and connected them in IB. Clicking on the second Tab (e.g. sndMenuVC) opens a view with 4 buttons. Clicking one of these buttons leads to another view (e.g. detailVC) in which I don't want the TabBar to be displayed. detailVC has it's own nib.

Opening detailVC happens with the button's action declared like this

- (IBAction) openFourth:(id)sender{
detailVC *detailView = [[detailVC alloc] initWithNibName:@"detailVC" bundle:nil andSender:kFourthButtonSender];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}

As this is a custom initWithNibName, this is how I implemented it

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andSender: (NSString *)calledByButton{
self.callingButton = calledByButton;
[super initWithNibName:@"detailVC" bundle:nil];
return self;
}

SO I basically just set a global variable "callingButton" according to the transmitted sender and call the "normal" initWithNibName afterwards.

This all works fine.

If I try to call hideAlsoCustomTabBar, and NSLog the value of btn1.hidden it returns 0 when called from detailVC, but returns 1 when called from within CustomTabBar and actually hides the buttons.

I have customTabs as IBOutlet if needed, but don't know if this is connected correctly to the TabBarController of type CustomTabBar.

Buttons won't hide in custom TabBar

Hope this helps to understand me :) If you need any other information, just let me know. Thanks!


I have written a follow up to my RXCustomTabBar tutorial which should answer your questions. I don't see any point reproducing it in full here.

Hiding your New Custom UITabBar (RXCustomTabBar follow up)

Let me know if you have any questions, Oli


if you want to hide the tabBar you can simply, in your view controller, call

[[[self tabBarController] tabBar] setHidden:YES];


In RXCustomTabBar.m file addCustomElements function is called from viewDidAppear.

-(void)addCustomElements
{
    .
    .
    .
    .


    // Add my new buttons to the view
    // Following lines are adding buttons to the view. Put your condition here according to requirement so that it will check and add buttons accordingly.
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:btn4];

    .
    .
    .
    .

}

Hope this helps.

Update

//Add following function in RXCustomTabBar.m
-(void)hideButtons
{
    btn1.hidden = YES;
    btn2.hidden = YES;
    btn3.hidden = YES;
    btn4.hidden = YES;
}

- (void)selectTab:(int)tabID
{
    switch(tabID)
    {
        case 0:
            [btn1 setSelected:true];
            [btn2 setSelected:false];
            [btn3 setSelected:false];
            [btn4 setSelected:false];
            break;
        case 1:
            [self hideButtons]; //Call function for hiding buttons like this.
            [btn1 setSelected:false];
            [btn2 setSelected:true];
            [btn3 setSelected:false];
            [btn4 setSelected:false];
            break;
    }   
    self.selectedIndex = tabID;
}

Let say you want to hide buttons on 2nd View Controller. So I have called [self hideButtons] in case 1 index.

This will Hide all the tabbar buttons. And again Viceversa you have to put diff condition to show those tab buttons.

Does this make sense ?


Since the custom tabs exists already at the point this method is called. You should assign it to detailView's customTabs property here.

- (IBAction) openFourth:(id)sender{
    detailVC * detailView = [[detailVC alloc] initWithNibName:@"detailVC" bundle:nil andSender:kFourthButtonSender];
    detailView.customTabs = **theExistingCustomTabsObject**;
    [self.navigationController pushViewController:detailView animated:YES];
    [detailView release];
}

Orignal Answer
As the reference to the subclass of the UITabBarController is nil, you will have to set it properly. If you have used IB to set the view controllers, go to the NIB file. Right click on the view controller, select the customTabs outlet and connect it to the customTabBar object holding the view controllers.

If you've created the view controllers programmatically then somewhere after adding the view controllers to the tab bar and before releasing the view controllers, do this,

viewController.customTabs = self.customTabBarObject;
0

精彩评论

暂无评论...
验证码 换一张
取 消