import SwiftUI struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() .alignment(.top) } } extension View { @ViewBuilder func alignment(_ alignment: Alignment) -> some View { switch alignment { case .leading: self.frame(maxWidth: .infinity, alignment: .leading) case .trailing: self.frame(maxWidth: .infinity, alignment: .trailing) case .top: self.frame(maxHeight: .infinity, alignment: .top) case .bottom: self.frame(maxHeight: .infinity, alignment: .bottom) case .topLeading: self .frame(maxHeight: .infinity, alignment: .top) .frame(maxWidth: .infinity, alignment: .leading) case .topTrailing: self .frame(maxHeight: .infinity, alignment: .top) .frame(maxWidth: .infinity, alignment: .trailing) case .bottomLeading: self .frame(maxHeight: .infinity, alignment: .bottom) .frame(maxWidth: .infinity, alignment: .leading) case .bottomTrailing: self .frame(maxHeight: .infinity, alignment: .bottom) .frame(maxWidth: .infinity, alignment: .trailing) default: self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) } } } #Preview { ContentView() }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter