SwiftUI button to change view - swiftui

I followed this tuto : https://www.youtube.com/watch?v=mUMR5TCqpDU
Until the end it's ok. But now I would like to add a button.
So I created : bouton1 and 2 at the top
And this is the header of my page : headerview
I would like to go on another view when I click on Bouton2 for example. I guess it's a basic functionality, BUT I when I add navigationview that change everything on my page..
I guess it's because I have a header that should be different..
I tried with
Here my HeaderView.swift
import SwiftUI
struct HeaderView: View {
var body: some View {
NavigationView {
Text("ttt").navigationBarItems(leading:
NavigationLink(destination: GarageView()) {
Text("ey")
},
trailing:
Button(action: {}) {
Text("ee")
}
)
}
.accentColor(.green)
}
}
struct HeaderView_Previews: PreviewProvider {
static var previews: some View {
HeaderView()
.previewLayout(.fixed(width: 375, height: 300)) //375 80
}
}
and my contentView.swift :
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
// Top Stack
HeaderView()
// Card
ZStack {
ForEach(Card.data.reversed()) { card in
CardView(card: card).padding(10)
}
}.zIndex(1.0)
// Bottom Stack
FooterView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
but the result is : I don't know why the header is big like this then i click on the button "ey" -> as you can see the new page is the half of my screen
I would like my new page take the entire screen...
Do you have an idea ?
Thank you.

Related

TabView's dots cannot be hidden with an animation

I have a TabView with a .page style. I added a button to show / hide the dots with an animation.
When I click the button to display the dots, the animation plays smoothly. However, when I click the button to hide the dots, they disappear instanlty.
The button's text changes if the dots are visible/hidden. Weirdly, both animations are working on the text.
What did I miss? Why is the animation not working as expected?
Here is the code :
import SwiftUI
struct ContentView: View {
#State private var activeTab : Int = 0
#State private var showDots = true
var body: some View {
VStack {
TabView(selection: $activeTab) {
Text("Foo").tag(0)
Text("Bar").tag(1)
}
.tabViewStyle(.page(indexDisplayMode: showDots ? .always : .never))
.indexViewStyle(.page(backgroundDisplayMode: showDots ? .always : .never))
Button(action: {
withAnimation(.linear(duration: 1)) {
showDots.toggle()
}
}) {
Text(showDots ? "Hide dots" : "Show dots")
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thanks

Unable to get the same UI for the iPAD [duplicate]

Is there a way to disable the SplitView using SwiftUI on iPad inside a navigation view?
By setting the NavigationViewStyle
import SwiftUI
struct NavView: View {
var body: some View {
NavigationView{
List{
NavigationLink(destination: TestView(), label: {Text("TestView")})
}
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct NavView_Previews: PreviewProvider {
static var previews: some View {
NavView()
}
}

SwiftUI: NavigationLink pops out immediately on WatchOS 8.1RC in Tabview

I have discovered a regression in watchOS 8.1RC with NavigationLink triggered from a TabView.
It's immediately dismissed.
It was working in watchOS 8.0 or in Simulator (watchOS 8.0).
Do you know a workaround ?
Thanks
Sample code:
import SwiftUI
#main
struct TestNavigationApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
}
struct ContentView: View {
var body: some View {
List {
NavigationLink(destination: ContentView1()) {
Text("To TabView")
}
}
}
}
struct ContentView1: View {
var body: some View {
TabView {
NavigationView {
NavigationLink(destination: ContentView2()) {
Text("To ContentView2")
}
}
VStack {
Text("Screen2")
}
}
}
}
struct ContentView2: View {
var body: some View {
Text("ContentView2")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I'm experiencing the same issue with watchOS 8.1 (and 8.3 beta) while it was working with previous watchOS versions.
We were able to get it working again by moving the NavigationView inside the TabView. This workaround isn't ideal at all but it does seem to work.
#State private var tabSelection = 1
var body: some Scene {
WindowGroup {
TabView(selection: $tabSelection) {
NavigationView {
// List goes here
}
.tag(1)
VStack(alignment: .center, spacing: 12, content: {
// content 2nd tab: we didn't have a list in the 2nd tab
})
.tag(2)
}
}
}
However, there are 2 things impacted with this fix:
I didn't get the navigationBarTitle working, so there won't be a title on top of the screen.
If you click on an item in the list, it will navigate to your page (as expected) but the TabView dots at the bottom of the screen will remain.

Creating nav link error in Swift UI/ Xcode

I am trying to add a Navigation view that goes from my ContentView to my list items inside of my SectionView and then goes to my DetailView...i'm getting an error and i'm not sure why? im trying to nav link SelectionView( codeName: "Y")
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("Y", destination:codeName.
(model: .codeName))
List (models) { model in
SelectionView(codeName: model.codeName)
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.device)
.preferredColorScheme(.dark)
.previewDevice("iPhone 13 Pro Max")
}
}
#endif
}
}
The destination also should be a view.
struct ContentView: View {
var body: some View {
NavigationView {
List {
ForEach(0 ..< 5) { item in
NavigationLink(
destination: Text("Destination \(item)"),
label: {
Text("Hello, world! \(item)")
.padding()
})
}
}
}
}
}

Why does the SwiftUI "Form" cause my button to go to the bottom of the screen?

I want to create a view that has a form with a button below it. If I include a form and a button, the button goes to the bottom of the screen.
Without Form Element
With Form Element
Is this just a SwiftUI bug? Or am I doing something wrong?
//
// TestFile.swift
// searchparty
//
// Created by Me
//
import SwiftUI
struct TestFile: View {
var body: some View {
VStack {
Form{
Text("Hello, World!")
}
Button("Button") {
print("Button tapped!")
}
}
}
}
struct TestFile_Previews: PreviewProvider {
static var previews: some View {
TestFile()
}
}
An empty Section with a footer will do the job, although you'll want to explicitly set the font, or else the footer will change it based on the form footer environment:
struct ContentView: View {
var body: some View {
Form {
Text("Entry")
Section(footer:
HStack {
Spacer()
Button(action: {}) {
Text("My button")
.font(.system(.body))
}
Spacer()
}
) {
EmptyView()
}
}
}
}