0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】アイコン画像の色を変える

Posted at

完成形

IMG_7921.PNG
IMG_7920.PNG

修正前

IMG_7925.PNG
IMG_7926.PNG

UIImageView の場合

色を指定する

色は TintColor に従うので、 TintColor を変更することで色を変えることができます。

class WithIconTableViewCell: UITableViewCell {

    @IBOutlet var leftImageView: UIImageView!
    @IBOutlet var centerLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // アイコン画像の色を指定する
        leftImageView.tintColor = .TextColor
    }

画像のα値だけで描画させる

UIImage で指定できる UIImageRenderingMode.AlwaysTemplate は画像のα値だけで描画してくれます。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            switch indexPath.row {
            case 0:
                //① UI部品を指定 TableViewCell
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WithIconTableViewCell
                cell.centerLabel.text = "アップグレード"
                cell.leftImageView.image = UIImage(named: "icons8-シェブロン-25")?.withRenderingMode(.alwaysTemplate)
                return cell
            default:
                return WithIconTableViewCell()
            }
        }

UIButton の場合

            // 画像のα値だけで描画させる
            let backImage = UIImage(named: "icons8-戻る-25")?.withRenderingMode(.alwaysTemplate)
            Button_Left.setImage(backImage, for: UIControl.State.normal)
            // アイコン画像の色を指定する
            Button_Left.tintColor = .TextColor
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?