UIBarButtonItem not change image automatically when switching dark mode/light mode

Hi all,


I am trying to integrate dark mode support to my app. But when setting image icon for UIBarButtonItem, it seem only works when the first time UIBarButtonItem is shown, it is not changed when I switch between dark mode/light mode.


When using that image with other UIButton, it works fine.


P/s: I have to use that trick for updating image:


        let item: UIBarButtonItem = UIBarButtonItem()
        let button: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 26, height: 19))
        button.setImage(UIImage(named: "hamburger"), for: .normal)
        button.addTarget(self, action: action, for: .touchUpInside)
        item.customView = button

Replies

Here you will find a way around this issue:

[(https://stackoverflow.com/a/63547257/8738937)]
Had the same issue, if you are utilizing pdf resources for images add .withRenderingMode(.alwaysTemplate) to your image.

Some people have had success by doing .withRenderingMode(.alwaysOriginal)