どうも、ちょげ(@chogetarou)です。
画像のImageのサイズを、アスペクト比を保ったまま余白にフィットさせる方法を紹介します。
方法

画像のサイズをアスペクト比を保ったままフィットさせるには、aspectRatio修飾子を使います。
まず、Imageにresizable修飾子を付与します。
そして、ImageにaspectRatio修飾子を付与します。
aspectRatioの引数「contentMode」には、「.fit」を指定します。
Image("image")
.resizable()
.aspectRatio(contentMode: .fit)
引数「contentMode」に「.fit」を指定したaspectRatio修飾子を付与することで、Imageをアスペクト比を保ったまま余白にフィットさせることが出来ます。
使用例

struct ContentView: View {
var body: some View {
VStack {
Image("DogPhoto")
.resizable()
.aspectRatio(contentMode: .fit)
}
}
}
コメント