如何在 interactivePopGesture 时处理 UITabBarController 的子视图(如 Flipboard)

How to handle subviews of UITabBarController when interactivePopGesture (like Flipboard)

我想要一个下划线来指示选择了哪个项目。只要点击该项目,它就会滑到任何其他项目。因此,我在自定义UITabBarController中添加了一个subview,并设置了动画。然后我使用 hidesBottomBarWhenPushed 在按下时隐藏标签栏。但是,下划线似乎没有与自定义组合 UITabBarController.

如何处理子视图使其始终在顶部,即使使用后退手势也是如此?这个 Flipboard 应用捕获是我想要做的。

编辑:

CustomTabBarController.m
- (void)viewDidLoad
{
    [super viewDidLoad];

    // create underline view
    CGRect tabBarFrame = self.tabBar.frame;
    CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarFrame) / MIN(5, self.tabBar.items.count);
    CGFloat originX = (CGFloat)itemWidth * self.selectedIndex;
    CGRect underlineFrame = CGRectMake(originX, CGRectGetMaxY(tabBarFrame) - 3.0f, itemWidth, 3.0f);

    self.underlineView = [[UIView alloc] initWithFrame:underlineFrame];
    self.underlineView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.underlineView];
}

#pragma mark - UITabBarDelegate

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSUInteger itemIndex = [tabBar.items indexOfObject:item];
    CGRect underlineFrame = self.underlineView.frame;
    CGFloat originX = (CGFloat)CGRectGetWidth(self.underlineView.frame) * itemIndex;

    // underline shifting animation
    [UIView animateWithDuration:0.25
                     animations:^{
                         self.underlineView.frame = CGRectMake(originX, underlineFrame.origin.y, CGRectGetWidth(underlineFrame), CGRectGetHeight(underlineFrame));
                     }];
}

CustomTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *detailViewController = segue.destinationViewController;
    detailViewController.hidesBottomBarWhenPushed = YES;
}

hidesBottomBarWhenPushed 隐藏标签栏但它的子视图(下划线视图)。 如果我自己隐藏显示在viewWillAppear,下划线视图看起来不像在标签栏的顶部。

我终于找到了解决方法。要覆盖方法 hidesBottomBarWhenPushed,则可以为选项卡栏的子视图添加替代视图。

sourceViewController.m

- (BOOL)hidesBottomBarWhenPushed
{
    [super hidesBottomBarWhenPushed];

    CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;

    if (tabBarController.underlineView.isHidden) {

        CGRect tabBarBounds = tabBarController.tabBar.bounds;
        CGFloat underlineHeight = CGRectGetHeight(tabBarController.underlineView.frame);
        CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarBounds) / MIN(5, tabBarController.tabBar.items.count);
        CGFloat originX = (CGFloat)itemWidth * tabBarController.selectedIndex;

        UIView *alternativeView = [[UIView alloc] initWithFrame:CGRectMake(originX, 
                                                                CGRectGetMaxY(tabBarBounds) - underlineHeight,
                                                                itemWidth,
                                                                underlineHeight)];
        alternativeView.tag = tabBarController.underlineViewTag;
        alternativeView.backgroundColor = tabBarController.underlineView.backgroundColor;
        [tabBarController.tabBar addSubview:alternativeView];
    }

    return NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;

    if (tabBarController.underlineView.isHidden) {
        tabBarController.underlineView.hidden = NO;

        NSInteger underlineViewTag = tabBarController.underlineViewTag;
        UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
        [alternativeView removeFromSuperview];
    }
}

不要忘记 interactivePopGesture 弹出视图控制器失败的情况,替代视图仍会添加到选项卡栏。因此,如果需要,请在目标视图控制器中将其删除。

destinationViewController.m

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
    NSInteger underlineViewTag = tabBarController.underlineViewTag;
    UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];

    if (alternativeView) [alternativeView removeFromSuperview];
}