1

我想要一个下划线来指示选择了哪个项目。每当点击该项目时,它就会滑动到任何其他项目。因此,我在自定义中添加了一个子视图UITabBarController并设置了动画。然后我用来hidesBottomBarWhenPushed在推送时隐藏标签栏。但是,下划线似乎没有与 custom 结合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,则下划线视图看起来不像在标签栏顶部。

捕获

4

1 回答 1

0

我终于找到了解决方法。要覆盖该方法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];
}
于 2015-05-04T15:36:51.047 回答