1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

モディファイアの適用・非適用を実装する

1
Posted at

きっかけ

特定の条件下でのみ、画像の色を変更したいという場合があったので、
それを実装するために調べてみた。

方法

extension View {
@ViewBuilder
    func `if`<Content: View>(
      _ condition: Bool, transform: (Self) -> Content
    ) -> some View {
        if condition {
            transform(self)
        } else {
            self
        }
    }
}

呼び出し方

ContentView.swift
Image("testImage")
  .renderingMode(.template)
  .if(viewModel.iconName == "testIcon1") {
     $0.foregroundColor(Color.blue)
  }
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?