0

How to get imageView from UITabBar specific items. From below code its highlight enter specific section View.

I need to highlight More Tab only UIImageView for CoachMark.

How to get UIImageView value from UITabBar ?

        guard let view = self.tabBarController?.tabBar.items?.last?.value(forKey: "view") as? UIView else{
            return coachMarksController.helper.makeCoachMark()
        }
       // let imageView = view.subviews.compactMap { $0 as? UIImageView }.first
       // return coachMarksController.helper.makeCoachMark(for: imageView) // Fails

        return coachMarksController.helper.makeCoachMark(for: view)

enter image description here

kiran
  • 4,285
  • 7
  • 53
  • 98

1 Answers1

0

Add these extensions to your app.

import UIKit

public extension UITabBar {
    var tabBarButtons: [UIView] {
        let values = self.subviews.filter({ String(describing: type(of: $0)) == "UITabBarButton" })
        return values.sorted(by: { $0.frame.origin.x < $1.frame.origin.x })
    }
}

public extension UIView {
    var tabBarSwappableImageViews: [UIView] {
        let values = self.subviews.filter({ String(describing: type(of: $0)) == "UITabBarSwappableImageView" })
        return values.sorted(by: { $0.frame.origin.x < $1.frame.origin.x })
    }
}

Then call this on your tab bar controller

let imageView: UIImageView? = UITabBarController?.tabBar.tabBarButtons[YOUR_INDEX_HERE].tabBarSwappableImageViews.first
Brandon Stillitano
  • 1,304
  • 8
  • 27
  • For first index its perfectly worked. But when used second index its failing return empty array. self.tabBarController?.tabBar.tabBarButtons[1].tabBarSwappableImageViews ▿ Optional> - some : 0 elements – kiran Dec 17 '22 at 19:48