iOS - 获取 UITabBarItem UIImageView/UIImage 的框架

iOS - Getting frame of the UIImageView/UIImage of a UITabBarItem

我正在尝试获取 UITaBarItem 中显示的 imageframe。我可以获得任何 UITabBarItemview,但无法获得任何 UITabBarItem 的仅图像部分的帧。添加我的观点的屏幕截图

其中一个简单的解决方案是

guard let view = self.tabBarVC?.tabBar.items?[0].valueForKey("view") as? UIView 
else 
 {
    return
 }
let frame = view.frame

extension UITabBar {

func getFrameForTabAt(index: Int) -> CGRect? {
    var frames = self.subviews.compactMap { return [=11=] is UIControl ? [=11=].frame : nil }
    frames.sort { [=11=].origin.x < .origin.x }
    return frames[safe: index]
  }

}

extension Collection {

subscript (safe index: Index) -> Element? {
    return indices.contains(index) ? self[index] : nil
}

}

获得 UIBarButtonItem 的观看次数

public extension UIBarButtonItem {

    var view: UIView? {
        guard let view = self.value(forKey: "view") as? UIView else {
            return nil
        }
        return view
    }
}

UIBarButtonItem 视图获取 UIImageView:

let barItemImageView = self.navigationItem.rightBarButtonItem?.view.subviews.filter { view in
        view is UIImageView
        }.first

这将return我右边的UIBarButtonItem

的UIImageView