Swift 画像のサイズをアスペクト比を維持するサンプル

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

構文
Image(“image")
.resizable()
.aspectRatio(contentMode: .fit)
引数「contentMode」に「.fit」を指定したaspectRatio修飾子を付与します。
Imageをアスペクト比を保ったまま余白にフィットさせます。

サンプル

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
var body: some View {
VStack {
Image("testo")
.resizable()
.aspectRatio(contentMode: .fit)
}
}
}
struct ContentView: View { var body: some View { VStack { Image("testo") .resizable() .aspectRatio(contentMode: .fit) } } }
struct ContentView: View {
    var body: some View {
        VStack {
           Image("testo")
                .resizable()
                .aspectRatio(contentMode: .fit)
        }
    }
}

 

Swift

Posted by arkgame