     }
 
     open override var selectedViewController: UIViewController? {
-        didSet {
-            guard let vc = selectedViewController,
+        willSet {
+            guard let vc = newValue,
                 let index = viewControllers?.firstIndex(of: vc) else { return }
             handleSelection(index: index)
         }

    }
    
    // MARK: actions
    private func handleSelection(index: Int) {
        guard let items = tabBar.items as? [RAMAnimatedTabBarItem] else { return }
        let currentIndex = index

            let renderMode = item.iconColor.cgColor.alpha == 0 ? UIImage.RenderingMode.alwaysOriginal :
                UIImage.RenderingMode.alwaysTemplate

            
            let iconImage = item.image ?? item.iconView?.icon.image
            let icon = UIImageView(image: iconImage?.withRenderingMode(renderMode))
            icon.tintColor = item.iconColor
            icon.highlightedImage = item.selectedImage?.withRenderingMode(renderMode)
            container.addSubview(icon)

            
            let textLabel = UILabel()
            if let title = item.title, !title.isEmpty {
                textLabel.text = title
