隐藏自定义工具栏按钮

时间:2016-12-12 05:18:07

标签: ios objective-c tabbar

我创建了一个自定义标签栏,并在中心位置添加了自定义图像。以下代码按预期工作。它将自定义按钮添加到选项卡栏的中心。

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;
}

我面临的问题如下。有一个viewcontroller - ChatViewContoller我隐藏tabbar。但由于tabbar上添加了自定义按钮,因此我无法使用以下代码隐藏自定义按钮。

ChatViewController.m

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

1 个答案:

答案 0 :(得分:1)

Hotspring,请参考原始tabbar对象,而不是在隐藏中心按钮时创建一个新对象,即

CustomTabBarViewController* tab = //reference to existing tabbar 

而不是

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

然后尝试隐藏按钮