Getting the frame of a particular tab bar item

前端 未结 12 1041
礼貌的吻别
礼貌的吻别 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:56

    I'll add what worked for me in my simple UITabBarController scenario, everything is legit but it has an assumption that items are spaced equally. Under iOS7 it returns an instance of an UITabBarButton, but if you'll be using it as UIView* you really don't care what it is and you aren't stating the class explicitly. The frame of the returned view is the frame you're looking for:

    -(UIView*)viewForTabBarItemAtIndex:(NSInteger)index {
    
        CGRect tabBarRect = self.tabBarController.tabBar.frame;
        NSInteger buttonCount = self.tabBarController.tabBar.items.count;
        CGFloat containingWidth = tabBarRect.size.width/buttonCount;
        CGFloat originX = containingWidth * index ;
        CGRect containingRect = CGRectMake( originX, 0, containingWidth, self.tabBarController.tabBar.frame.size.height );
        CGPoint center = CGPointMake( CGRectGetMidX(containingRect), CGRectGetMidY(containingRect));
        return [ self.tabBarController.tabBar hitTest:center withEvent:nil ];
    }
    

    What it does is calculate the rect in the UITabBar where the button resides, finds the center of this rect and digs out the view at that point via hitTest:withEvent.

提交回复
热议问题