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
Asked
Active
Viewed 755 times
-1

Md. Sulayman
- 781
- 8
- 30
-
You can get the size of tabBarItem image using `self.tabBarItem.selectedImage?.size` – Nilesh R Patel Feb 27 '19 at 07:01
-
@NileshRPatel I needed the frame. I have found a solution , fixed the problem – Md. Sulayman Feb 27 '19 at 08:52
2 Answers
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
-
I know about your first solution. I am using it. I need the uiimage part only – Md. Sulayman Feb 27 '19 at 06:54