I'm trying to align "Top Text" to the top of the screen, and I can't find a way to do it. "Top Text" needs to be aligned near the "notch of the iphone screen.
I attached a screenshot of where "Top Text" is currently, and needs to be moved to the top
struct ContentView: View {
//var newColor : [String: Double] = setColor(red:247, green:186, blue:161)
var body: some View {
ZStack {
VStack(spacing: 0) {
HStack {
Text("Top Text[![enter image description here][1]][1]")
.fontWeight(.light)
.multilineTextAlignment(.center)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 100)
.font(.body)
.padding()
}
.offset(y: 0)
.frame(minWidth: 0, maxHeight: 400, alignment: .topLeading)
VStack {
Text("Sign in with Facebook")
.fontWeight(.light)
.font(.title)
.frame(minWidth: 0, maxWidth: .infinity, maxHeight: 50)
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
Text("Sign in with Google")
.fontWeight(.light)
.font(.title)
.padding()
.frame(minWidth: 0, maxWidth: .infinity, maxHeight: 50)
}
}
.foregroundColor(Color.black.opacity(0.7))
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.offset(x: 0, y: 0)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.background(Color.orange.opacity(0.2))
.edgesIgnoringSafeArea(.all)
}
}
SwiftUI gives us a number of valuable ways of controlling the way views are aligned, and I want to walk you through each of them so you can see them in action. You can then use offset(x:y:) to move the text around inside that frame.
Top alignment - Alt + H then A + T. Middle alignment - Alt + H then A + M. Bottom alignment - Alt + H then A + B. Left alignment - Alt + H then A + L.
Here is possible approach
var body: some View {
ZStack(alignment: .top) { // << made explicit alignment to top
HStack { // << moved this up to ZStack
Text("Top Text")
.fontWeight(.light)
.multilineTextAlignment(.center)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 100)
.font(.body)
}
.frame(minWidth: 0, maxHeight: 400, alignment: .topLeading)
VStack(spacing: 0) {
VStack {
Text("Sign in with Facebook")
.fontWeight(.light)
.font(.title)
.frame(minWidth: 0, maxWidth: .infinity, maxHeight: 50)
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
Text("Sign in with Google")
.fontWeight(.light)
.font(.title)
.padding()
.frame(minWidth: 0, maxWidth: .infinity, maxHeight: 50)
}
}
.foregroundColor(Color.black.opacity(0.7))
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.offset(x: 0, y: 0)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.background(Color.orange.opacity(0.2))
.edgesIgnoringSafeArea(.all)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With