2015-12-02 18 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}) 
}