[Xcode/SwiftUI] LabelとIconを等間隔にする方法(labelReservedIconWidth)

実装

VStackに対して以下のmodifierをつける

.labelReservedIconWidth(<#CGFloat#>)

サンプル

import SwiftUI

struct ContentView: View {

    private let sampleAppleDevices: [(name: String, imageName: String)] = [
        ("iPhone", "iphone"),
        ("iPad", "ipad"),
        ("Macbook", "laptopcomputer"),
        ("Apple TV", "tv"),
        ("AirPods", "airpodsmax")
    ]

    var body: some View {
        VStack(alignment: .leading, spacing: 24) {
            ForEach(sampleAppleDevices, id: \.0) { device in
                Label(device.name, systemImage: device.imageName)
                    .font(.system(size: 32))
            }
        }
        .labelReservedIconWidth(40)
    }
}