Search code examples
iosswiftuiimagepickercontrolleruibarbuttonitem

UIBarButtonItems are not visible


I have a UIViewController that allows the user to select a photo or a video via UIImagePickerController. Once they select one of them the image is being assigned to a property called var selectedImage (if they select a video, there's a method that generates a thumbnail for that video). It works as expected.

The problem is that I want to handle the state of 2 UIBarButtonItems. I want to disable them when selectedImage is nil and set the isEnabled once there's an image.

Pretty straight forward, right? Well, the problem is that initially it loads the UIViewController, the buttons are disabled and visible, then I select a photo from the gallery, didFinishPickingMediaWithInfo is called and in there, I also call handlePost() method that should take care of the state (enabled/disabled) of the buttons. Well, for some reason, even though there's an image in selectedImage, the buttons are not being displayed (practically invisible, but as far as I know, there's no isHidden for UIBarButtonItem.

They are still active, if i tap on them (despite not seeing them) they do their job. Now, the problem is why are not they visible? I can guarantee Colors.tint has a value (I use this class throughout the app, it works) and the selectedImage has a value (I've put a break point and it calls the handlePost() method, goes through the first if statement where that property has a value. Yet, I don't see the buttons being displayed. The question is why?

@IBOutlet weak var clearButton: UIBarButtonItem!
@IBOutlet weak var filterButton: UIBarButtonItem!

var selectedImage: UIImage?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    handlePost()
}

func handlePost() {
    if selectedImage != nil {
        clearButton.isEnabled = true
        clearButton.tintColor = Colors.tint
        filterButton.isEnabled = true
        filterButton.tintColor = Colors.tint
        shareButton.isEnabled = true
        shareButton.backgroundColor = Colors.tint
        shareButton.setTitleColor(.white, for: .normal)
    }
    else {
        clearButton.isEnabled = false
        clearButton.tintColor = .darkGray
        filterButton.isEnabled = false
        filterButton.tintColor = .darkGray
        shareButton.isEnabled = false
        shareButton.backgroundColor = .darkGray
        shareButton.setTitleColor(.lightGray, for: .normal)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
        if let thumbnail = self.generateThumbnailForImage(videoUrl) {
            self.videoUrl = videoUrl
            self.photoImageView.image = thumbnail
            self.selectedImage = thumbnail
            handlePost()
        }
    }
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        selectedImage = image
        photoImageView.image = image
        handlePost()
    }
    dismiss(animated: true, completion: nil)
}

Solution

  • As I mentioned in the comments, I'm not sure what's the issue because there's no problem for this to happen. But the simply solution was to remove the UIBarButtonItems and just replace them with UIButtons instead. This did the trick and now they're behaving the way they should.