-1

I am trying to get the frame of an image shown in a UITaBarItem. I can get the view of any UITabBarItem but can't able to get the frame of only image part of any UITabBarItem. Adding an screenshot of my views enter image description here

Md. Sulayman
  • 781
  • 8
  • 30

2 Answers2

1

For getting UIBarButtonItem's view

public extension UIBarButtonItem {

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

For getting UIImageView from UIBarButtonItem view:

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

This will return the UIImageView of my right UIBarButtonItem

Md. Sulayman
  • 781
  • 8
  • 30
0

One of the simple solution is

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
}

}
Shruti
  • 1,849
  • 1
  • 13
  • 21