1

我想要一個下劃線指示哪個項目被選中。只要物品被點擊,它就會滑動到任何其他物品上。因此,我在自定義UITabBarController中添加了一個子視圖並設置了動畫。然後我使用hidesBottomBarWhenPushed按下時隱藏標籤欄。但是,下劃線似乎沒有與定製UITabBarController結合使用。當interactivePopGesture(如Flipboard)時如何處理UITabBarController的子視圖

如何處理子視圖,使其始終位於頂端,即使使用後手勢?這Flipboard應用程序捕獲是我想要做的。

capture

編輯:

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中顯示,則下劃線視圖看起來不像標籤欄頂部。

capture

+0

您應該首先顯示一些代碼,以便我們提供幫助。我從圖中看不到爲什麼你的工作可能不正常 – 2015-03-12 09:21:37

回答

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]; 
} 
相關問題