2015-12-02 21 views
8

我使用一个名为SwiftyWalkthrough的外部库,它允许我只显示屏幕上的某些视图,以指导新用户通过我的应用程序的用户界面。我想要暴露给用户的第一个项目是UITabBarItem。我需要找到与特定UITabBarItem关联的UIView。我知道该项目的索引,并且给它一个标签。但我还没有找到一种方法让他们帮助我找到观点。当然,我只想使用公共接口来实现这一点。如何找到特定UITabBarItem的UIView?

+0

有标签栏项目很多意见。你在找哪一个? –

+0

在屏幕底部的标签栏上包含标题和图标的人。我期望它是矩形的,高度与整个标签栏相同,宽度等于标签栏宽度除以标签栏项目的数量。 –

+0

我认为没有访问UITabbaritem backgroundView,我们只能放置标签栏项目的自定义图像,但无法获取tabbar项目的backgroundView。 –

回答

19

我通过访问tabBar的子视图来解决我的问题。 userInteractionEnabled的视图是UITabBarItems。通过minX值对它们进行排序可以确保它们与tabBar.items数组的顺序相同。

extension UITabBarController { 
    func orderedTabBarItemViews() -> [UIView] { 
     let interactionViews = tabBar.subviews.filter({$0.isUserInteractionEnabled}) 
    return interactionViews.sorted(by: {$0.frame.minX < $1.frame.minX}) 
    } 
} 
+0

谢谢!真的很好,很优雅。我知道这可能会在未来破裂,但并不是一个更好的方法...... – elsurudo

1

测试中iOS10用Objective-C的:

[alert setModalPresentationStyle:UIModalPresentationPopover]; 

    UIView *view = [self.tabBar.selectedItem valueForKey:@"view"]; 

    alert.popoverPresentationController.delegate = self; 
    alert.popoverPresentationController.sourceView = self.tabBar; 
    alert.popoverPresentationController.sourceRect = view.frame; 
    alert.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown; 

    [self presentViewController:alert animated:YES completion:nil]; 
7

斯威夫特3版的卡尔 - 史密斯的答案

func orderedTabBarItemViews() -> [UIView] { 
    let interactionViews = tabBar.subviews.filter({$0.isUserInteractionEnabled}) 
    return interactionViews.sorted(by: {$0.frame.minX < $1.frame.minX}) 
}