I am attempting to program an iOS app with a settings button in the top right corner. I tried using a Spacer to get the SF Symbol image to stay in the top right but when I use a single spacer it pushes the image beyond the boundaries of the screen. Is it possible for me to add constraints to keep the image in the top right, without it going outside the boundaries of the screen? and if possible, to keep the image in the top right on all iOS devices? I am very new to SwiftUI so any insights and advice are appreciated!
Here is the code I am using:
import SwiftUI
struct ContentView: View {
var body: some View {
// view code
ZStack {
Image("Background").resizable()
.aspectRatio(contentMode: .fill).ignoresSafeArea()
VStack{
HStack {
Button(action: {}, label: {
Image(systemName: "gear").resizable().aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50) })
}
.padding()
Spacer()
HStack {
Button(action: {}, label: {
Image(systemName: "plus")
.resizable().aspectRatio(contentMode: .fit).frame(width: 150, height: 150, alignment: .center).padding().border(Color.white, width: 8).cornerRadius(10.0).foregroundColor(.white)
})
}
Spacer()
}
}
}
| With spacer | Without spacer |
|---|---|
![]() |
![]() |

