Getting the frame of a particular tab bar item

前端 未结 12 1039
礼貌的吻别
礼貌的吻别 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 08:06

    In Swift 4.2:

    private func frameForTab(atIndex index: Int) -> CGRect {
        var frames = view.subviews.compactMap { (view:UIView) -> CGRect? in
            if let view = view as? UIControl {
                return view.frame
            }
            return nil
        }
        frames.sort { $0.origin.x < $1.origin.x }
        if frames.count > index {
            return frames[index]
        }
        return frames.last ?? CGRect.zero
    }
    

提交回复
热议问题