Getting the frame of a particular tab bar item

前端 未结 12 1149
礼貌的吻别
礼貌的吻别 2021-02-02 07:42

Is there a way to find the frame of a particular UITabBarItem in a UITabBar?

Specifically, I want to create an animation of an image \"falling\

12条回答
  •  青春惊慌失措
    2021-02-02 07:45

    Swift + iOS 11

    private func frameForTabAtIndex(index: Int) -> CGRect {
        guard let tabBarSubviews = tabBarController?.tabBar.subviews else {
            return CGRect.zero
        }
        var allItems = [UIView]()
        for tabBarItem in tabBarSubviews {
            if tabBarItem.isKind(of: NSClassFromString("UITabBarButton")!) {
                allItems.append(tabBarItem)
            }
        }
        let item = allItems[index]
        return item.superview!.convert(item.frame, to: view)
    }
    

提交回复
热议问题