简体   繁体   中英

Swift Change UITabBarItem image and tint color dynamically

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! :)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM