casillas
casillas

Reputation: 16813

Hide Custom ToolBar Button

I have created a custom tabbar and added a custom image at the center position. The following code works as expected.It adds my custom button at the center of the tabbar bar.

CustomTabBarViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage* buttonImage = [UIImage imageNamed:@"icon_floating.png"];
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];

    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }

    [button addTarget:self
               action:@selector(myAction)
     forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

-(void) myAction
{
    self.selectedIndex = 2;
}

The issue that I have faced as follows. There is a viewcontroller - ChatViewContoller where I hide tabbar. But since the custom button is added on the tabbar, I could not able to hide the custom button with the following code.

ChatViewController.m

- (void)viewDidLoad {
  [super viewDidLoad];
  CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
  tab.button.hidden = YES;
}

Upvotes: 0

Views: 113

Answers (1)

Sanman
Sanman

Reputation: 1158

Hotspring,please reference the original tabbar object instead of creating a new one while hiding center button i.e

CustomTabBarViewController* tab = //reference to existing tabbar 

instead of

  CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];

and then try hiding button

Upvotes: 1

Related Questions