iOS-获取UITabBarItem的UIImageView / UIImage的框架

时间:2019-02-27 06:39:42

标签: ios swift uiimage frame uitabbaritem

我正在尝试让frame中显示的image中的UITaBarItem。我可以获取任何view中的UITabBarItem,但是无法获取任何UITabBarItem中仅图像部分的框架。添加我的视图的屏幕截图enter image description here

2 个答案:

答案 0 :(得分:0)

一种简单的解决方案是

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

OR

extension UITabBar {

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

}

extension Collection {

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

}

答案 1 :(得分:0)

用于获取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

这将返回我右边的UIBarButtonItem的UIImageView