SwiftUI About ZStack & NaviagtonLink - swiftui

I use ZStack & NaviagtonLink in SwiftUI.
ZStack{
List{
NavigationLink(destination: NextView()) {
Text("Hi")
}
}
Text("Hello")
}
This view is valid as I expected, and there are the text "Hello" on List's columns.
But even in NavigationLink ZStack is valid and in NextView there is hello.
I want to display it only this view, and I don't want to display it in NextView,
Please tell me how to fix.
Thank you.

Embed in navigationView otherwise navigation link cannot work
struct ContentView: View {
var body: some View {
NavigationView{
ZStack{
List{
NavigationLink(destination: NextView()) {
Text("Hi")
}
}
Text("Hello")
}
.navigationBarHidden(true)
}
}
}

Related

Unexplained Gap in SwiftUI View

I am seeing a white line between two of my view elements, that I cannot explain.
The following cod is the main SwiftUI View
var body: some View {
NavigationView {
VStack {
TextField("Filter", text: $lastNameFilter)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.top, .leading, .trailing, .bottom])
.background(Color(UIColor.systemGroupedBackground))
FilteredList(filter: lastNameFilter)
} }
The FilteredList view is very simple:
var body: some View {
List {
ForEach(fetchRequest, id: \.self) { recipient in
NavigationLink(destination:
ViewEventsView(recipient: recipient)) {
Text("\(recipient.wrappedFirstName) \(recipient.wrappedLastName)")
.foregroundColor(.green)
}
}
.onDelete(perform: deleteRecipient)
}
}
I have tried with and without padding, but that is not the issue. The .padding, is adjusting the inset of the "filter" TextField.
Any pointers would be appreciated.
That's probably the default spacing of the VStack. Try changing it to VStack(spacing: 0).

How to make this simple view with swiftui?

I am trying to do this view with swiftui but i am stuck.
I want the text("Mes évènements") to be centered and I want it to take all the place it can.
The two horizontal line should only take the place left.
I tried with HStack but I couldn't make it work as i would like to.
Here is a possible solution.
struct ContentView: View {
var body: some View {
HStack{
VStack{
OrangeLine()
}
Text("Mes évènements")
.font(.subheadline)
.fontWeight(.bold)
.foregroundColor(Color.orange)
VStack{
OrangeLine()
}
}
}
}
struct OrangeLine: View {
var body: some View {
Rectangle()
.fill(Color.orange)
.frame(height: 2)
.edgesIgnoringSafeArea(.horizontal)
}
}

Hidden Navbar still pushes down view

I have a:
contentView()
SignUpView()
SignInView()
The contentView calls the SignInView()
struct ContentView: View {
var body: some View {
NavigationView {
SignInView()
}
}
}
In my SignUpView() I have:
var body: some View {
VStack(alignment: .leading) {
NavigationLink(destination: SignInView()) {
Text("Sign in")
.fontWeight(.semibold)
.foregroundColor(Color("startColor"))
}
}.navigationBarHidden(true)
In my SigbInView I have:
var body: some View {
VStack(alignment: .leading) {
NavigationLink(destination: SignUpView()) {
Text("Sign up")
.fontWeight(.semibold)
.foregroundColor(Color("startColor"))
}.navigationBarHidden(true)
Im using .navigationBarHidden(true) to hide the bar, but the < back still appears in the top left hand corner to take you back to the previous screen, Iv also tried adding the navbar text = "" and setting the property to .inline
Im trying to only use these navigationLinks on the SignInView and SignUpViews to navigate, i don't want the bar to appear or push the view down.
So it looks like another property can be set to true to hide the back button:
.navigationBarBackButtonHidden(true)
This worked for me.

swiftui list not working when put into ZStack

I use ZStack to combine a list and Color, after doing it, List will not scroll and there's no output when clicking the text.
Does anyone know how to fix it?
Thanks
struct ContentView: View {
var body: some View {
ZStack{
List{
ForEach(1...30, id: \.self){ i in
Text("ROW \(i)")
.font(.system(size: 40))
.onTapGesture {
print("clicked \(i)")
}
}
}
Color.black.opacity(0.2)
}
}
}
Move Color before List and it will work. See the altered code below.
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{
Color.black.opacity(0.2)
List{
ForEach(1...30, id: \.self) { i in
Text("ROW \(i)")
.font(.system(size: 40))
.onTapGesture {
print("clicked \(i)")
}
}
}
}
}
}
I don't know why it isn't working — probably a bug — but you can (and probably should) do this instead.
struct ContentView: View {
var body: some View {
List{
ForEach(1...30, id: \.self){ i in
Text("ROW \(i)")
.font(.system(size: 40))
.onTapGesture {
print("clicked \(i)")
}
}
}.background(Color.black.opacity(0.2))
}
}

Hide Bottom Tab Bar on some view

I am trying to open one Contentview with NavigationLink.
But I dont get how to hide bottom tab bar when some view gets appear. I tried looking for code everywhere. but couldn't find anything helpful.
NavigationLink(destination: ItemDetail(item: item)){
}
that is how i open new view
This is to little code, but assuming you have a TabView and inside one of the TabView elements you have an NavigationLink, then you can hide the TabView for a specific view by adding the .navigationBarHidden(_ hidden: Bool) modifier.
https://developer.apple.com/documentation/swiftui/view/3338624-navigationbarhidden
Example:
struct ContentView: View {
var body: some View {
NavigationView {
TabView {
NavigationLink(destination: Text("NavigationLinkView")){
Text("NavigationLink")
}
.navigationBarHidden(true)
.tabItem {
Text("First View")
}.tag(0)
Text("Second View")
.tabItem {
Text("Second View")
}.tag(1)
}
}
}
}