Swift backgroundを使って画像Imageの背景色を設定するサンプル

環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)
Ubuntu 20.04.2 LTS

構文
Image(“xxx")
.background(color) //引数に背景色を指定する
Imageにbackground修飾子を付与します。
background修飾子の引数に背景色を指定します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "test")
.resizable()
.frame(width: 50, height: 50)
.padding()
.foregroundColor(.white)
.background(Color.yellow)
}
}
}
struct ContentView: View { var body: some View { VStack { Image(systemName: "test") .resizable() .frame(width: 50, height: 50) .padding() .foregroundColor(.white) .background(Color.yellow) } } }
struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "test")
                .resizable()
                .frame(width: 50, height: 50)
                .padding()
                .foregroundColor(.white)
                .background(Color.yellow)
        }
    }
}

 

Swift

Posted by arkgame