【问题标题】:Hide Custom ToolBar Button隐藏自定义工具栏按钮
【发布时间】:2023-03-23 14:38:01
【问题描述】:

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

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

我遇到的问题如下。有一个视图控制器 - ChatViewContoller 我隐藏了 tabbar。但是由于tabbar上添加了自定义按钮,所以我无法使用以下代码隐藏自定义按钮。

ChatViewController.m

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

【问题讨论】:

  • 尝试在chatVC viewdidload方法中引用已有的tabbar

标签: ios objective-c tabbar


【解决方案1】:

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

CustomTabBarViewController* tab = //reference to existing tabbar 

而不是

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

然后尝试隐藏按钮

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多