LoginSignup
0
0

configuration?.imageが効かない時の解決方法

Last updated at Posted at 2024-06-03

はじめに

以下Pixivさんのチュートリアルを行っているときにconfiguration?.imageが効かない時のケースに遭遇したのでメモ

解決方法

UIButtonのtypeをcustomからsystemに変更

systemにすることで、ボタンがシステム標準の処理を受けるようになり、UIButton.Configurationを使用して画像を設定した際に正しく反映されるようになりました。

UIButtonのtypeをcustomに設定していたことが原因でした。

customのまま画像が切り替わるようにする解決方法

setImage(_:for:)を使用する

heartButton.setImage(UIImage(systemName: illust.isFavorited ? "heart.fill" : "heart"), for: .normal)

この方法でもボタンの画像を切り替えることができます。

UIButtonのstate

UIButtonは異なる状態(state)に応じて異なる画像やタイトルを表示できます。
主な状態は以下です。

•	.normal: ボタンが通常の状態のとき
•	.highlighted: ボタンがタッチされているとき
•	.disabled: ボタンが無効のとき
•	.selected: ボタンが選択されているとき

おわりに

おかげでUIButtonのtypeやstateの理解が深まりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0