美文网首页
如何在tabBar的中间添加自定义按钮

如何在tabBar的中间添加自定义按钮

作者: songjk | 来源:发表于2021-03-12 15:13 被阅读0次

如何在tabBar的中间添加自定义按钮

1.在UITabBarController中间添加一个占位控制器

2.把UITabBarController的tabBar的中间的按钮设置为不可点击

    -(void)viewWillLayoutSubviews
    {
        [super viewWillLayoutSubviews];
        for (UIView *child in self.tabBar.subviews) {
            if ([child isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                NSString * title  = [child valueForKeyPath:@"label.text"];
                if (title && [title isKindOfClass:[NSString class]] && [title isEqualToString:@""]) {
                    child.userInteractionEnabled = NO;
                    return;
                }
            }
        }
    }

3.在添加完成所有子控制器后,在UITabBarController的tabBar的中间添加自定义按钮

-(void)addMidButton
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat height = self.tabBar.frame.size.height - [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
            UIButton * shot = [[UIButton alloc] initWithFrame:CGRectMake((self.tabBar.frame.size.width - height)*0.5, 0, height, height)];
            [shot addTarget:self action:@selector(showMidController) forControlEvents:UIControlEventTouchUpInside];
            [shot setBackgroundImage:[UIImage imageNamed:@"mid"] forState:UIControlStateNormal];
            shot.layer.cornerRadius = height * 0.5;
            [self.tabBar addSubview:shot];
        });
    }

相关文章

网友评论

      本文标题:如何在tabBar的中间添加自定义按钮

      本文链接:https://www.haomeiwen.com/subject/egjzqltx.html