Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you align text to the top of screen in SwiftUI

Tags:

swift

swiftui

enter image description hereI'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)

    }

}
like image 538
guest Avatar asked Jan 19 '20 02:01

guest


People also ask

How do I move text around in SwiftUI?

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.

What will you select in aligning the text to the top of the cell?

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.


1 Answers

Here is possible approach

enter image description here

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)

}
like image 148
Asperi Avatar answered Nov 04 '22 11:11

Asperi