0

I have a form that should be completed from the user, which is split between 3 UITabBarItems in 1 UITabBarController. I would like to change UITabBarItem's image dynamically. For example, the user is on the first step and right after he completes the configuration needed on that step I want the UITabBarItem that is responsible for this UIViewController to change its image to a tick indicating that the user can proceed to step two

I tried to set the tabBarItem in the current view controller to an image when all the values are completed, but it didn't work

if manufacturerCompleted
    && modelCompleted
    {
        let image = UIImage(named: "tick")
        self.tabBarItem.image = image?.withRenderingMode(.automatic)
    }

Thank you in advance! :)

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

Personally, I don't see any mistakes in the code. Maybe it doesn't get executed?

I suppose, you don't have an @IBAction function which gets called on the text change event (or whatever it is called). Try setting up one, connect it to the storyboard and then try again.

Oh, and a more 'swift-ey' way to handle optionals is the 'optional binding'. See more here.

var manufacturerCompleted = false
var modelCompleted = false

@IBAction func handleKeyDown(_ sender: UITextField) {
  updateManufacturerComplete(sender)
  updateModelComplete(sender)

  if manufacturerCompleted && modelCompleted {
    if let image = UIImage(named: "tick") as UIImage? {
      self.tabBarItem.image = image.withRenderingMode(.automatic)
    }
  }
}

func updateManufacturerComplete(_ sender: UITextField) {
  // Your condition here
  // ...
  self.manufacturerCompleted = true
}

func modelCompleted(_ sender: UITextField) {
  // Your condition here
  // ...
  self.modelCompleted = true
}
Fabian
  • 100
  • 11
  • I do all the work programmatically and the code is executed I am sure..checked it with a breackpoint –  Apr 21 '19 at 19:23
  • @PetioVishnevski Does the code get executed and nothing happens? Do you get any errors? What's the console output? If the code gets executed but nothing happens, there should be something in the console. [And have a look at this](https://stackoverflow.com/a/43802031/5738272), this user had the same question. – Fabian Apr 21 '19 at 20:06
  • The code is executed yes. I found the mistake...The UITabBarItem was created with a system icon, not with custom image...I changed the image successfully now. I have another problem. Should the image be for example 24x24 size or I can resize it somehow when setting it to the tabbar –  Apr 21 '19 at 20:14
  • I found this in apple developer documentation I think it will do the work https://developer.apple.com/documentation/uikit/uiimage/1624127-resizableimage Thank you guys. I am sorry I lost your time with my stupid mistake. –  Apr 21 '19 at 20:18
  • See this [page](https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/image-size-and-resolution/) for `UITabBar` image sizes. @PetioVishnevski – Fabian Apr 21 '19 at 21:02