How to change/define Text position - swiftui

If I have this code:
import SwiftUI
struct Test: View {
var body: some View {
Text("Hello World!")
.font(.title)
.foregroundColor(.white)
.padding()
.background(Color.red)
}
}
I have this result:
How can I position precisely from code "Hello World" in the upper part of the view, as shown in the screenshot below?

To move your Text to the top of the screen you need to wrap it in a VStack and place a Spacer below to push the text to the top of the screen:
var body: some View {
VStack {
Text("Hello World!")
// all the modifiers
Spacer()
}
}
Text takes only the amount of spaces it needs, while Spacer will occupy any space given to it.

Related

How to detect taps on a List cell row in SwiftUI?

Given a basic List with Text, how can i make the whole "cell" from left side of the screen to right, tappable in a List, not just the "Hello world" text?
List {
VStack {
Text("Hello world")
}
.contentShape(Rectangle())
.onTapGesture {
print("Tapped cell") // This only triggers when you directly tap the Text
}
}
Add a Button and entire cell is tappable now:
VStack {
Button(action: {
print("Tapped")
}) {
Text("Hello world")
}
}
Actually, all you really need to do is make sure the entire cell is filled with content. Changing the VStack to an HStack and adding a Spacer() will give you what you want.
List {
HStack {
Text("Hello world")
Spacer()
}
.contentShape(Rectangle())
.onTapGesture {
print("Tapped cell") // This triggers when you tap anywhere in the cell
}
}

How to move vstack at top of the screen in swiftui?

how to move this stack top of the screen in swiftUI.
below my code there.
[1]: https://i.stack.imgur.com/wBD2c.png
You just need to add Spacer() & embed your view in VStack & HStack to force push the view to the top.
VStack {
VStack {
//Your VStack
}
Spacer()
}
You can remove the HStack and place the Spacer() within your VStack which will force the view to the top.
var body: some View {
VStack {
Text("Hello World")
Text("Test")
Spacer()
}
}
Or if you want to keep the View to the left of the screen you can use another VStack and Spacer()
var body: some View {
VStack {
HStack {
VStack {
Text("Hello World")
Text("Test")
Spacer()
}
Spacer()
}
}
}
If you are just getting started with SwiftUI I highly recommend Paul Hudson's 100 Days of SwiftUI. It's free and will teach you a lot about SwiftUI.

Underline content of a VStack

I’m trying to add an line at the bottom of a VStack that fills to width of the VStack which is determined by the other content in the VStack, but the Rectangle I am using fills up the available space of the entire view.
struct ContentView: View {
var body: some View {
VStack {
Text("Testing123")
Rectangle().frame(height: 2)
}
}
}
How can I make the Rectangle only have the width necessary for the VStack to fit its content?
Here is one way to do it.
Put your Rectangle in an .overlay() of the VStack. Put the rectangle in its own VStack and use a Spacer to push the Rectangle to the bottom. Control the spacing between the rectangle and your original VStack by adding .padding to the last view in the VStack.
struct ContentView: View {
var body: some View {
VStack {
Text("Hi")
Text("Testing123")
Text("Bye").padding(.bottom, 10)
}
.overlay(
VStack {
Spacer()
Rectangle().frame(height: 2)
}
)
}
}

SwiftUI: Alignment not applied

I have a small example that U do not understand:
Why is the alignment of my ZStack not applied to all of it's children? The TopView stays on top but I would expect, that every child would be on bottom right:
struct ContentView: View {
var body: some View {
ZStack(alignment: .bottomTrailing) {
VStack {
TopView()
Spacer()
}
Text("A new layer")
}.padding()
}
}
struct TopView: View {
var body: some View {
HStack(alignment: .top) {
VStack {
Text("SwiftUI")
Text("Layout")
}
Spacer()
Image(systemName: "star")
}
}
}
By default all the views are in the middle of area they need. Even you're using VStack it will be in the middle of the screen and be with height of two Text (as in your example).
ZStack has the same behavior - it will be as small as possible and right in the center of safe area.
Spacer just under TopView tries to take all the free space. Just as Spacer between star and texts.
So VStack in your ZStack technically is on .bottomTrailing, but takes all the free space. Just try to remove or change position of Spacer's:

How do I change scrollview's scroll direction in SwiftUI?

I was trying to implement vertical scroll with image and text but I am not able to achieve it.
I tried on both Xcode beta 1 & 2.
Try to wrap both the Text and the Image in a VStack and be sure that there is enough content inside the ScrollView to fall outside it's bounds (in the right direction - vertically in your case):
ScrollView {
VStack {
ForEach (1...100) {_ in
Image(systemName: "circle.fill")
Text("my text")
}
}
}
You could easily try it out in a Playground like this :
import SwiftUI
import PlaygroundSupport
struct LiveView : View {
var body: some View {
ScrollView {
VStack {
ForEach (1...100) {_ in
Image(systemName: "circle.fill")
Text("Some text")
}
}
}
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: LiveView())