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

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()
}
}

Related

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()
})
}
}
}
}
}

NavigationLink in DisclosureGroup is not working when clicked

I'm trying to make a NavigationLink to "appleView()" swift file in a DisclosureGroup with the title "fruits".
But the NavigationLink is not working when clicked.
I don't know how to fix it.
I would appreciate it if anybody helped.
import SwiftUI
struct ContentView: View {
#State private var fruitsExpanded: Bool = false
var body: some View {
Form{
DisclosureGroup(
isExpanded: $fruitsExpanded,
content: {
List() {
NavigationLink(
destination: AppleView(),
label: {
Text("Apple")
})
Text("Banana")
}
},
label: { Text("Fruits") }
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

SwiftUI button to change view

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.

How can I use a common variable in a Tab VIew?

I'm currently developing an application using SwiftUI.
This app has 3 structs
①ContentView
②FirstView
③SecondView
These 3 structs do page transition in Tab View.
And this app has a common variable type of Bool using ObservableObject annotation.
I want to change to appear and disappear Text View in the FirstView and the SecondView depends on the condition of the variable, but the FirstView doesn't change a view as I expected...
How can I solve this situation?
Here are the codes:
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
FirstView.swift
import SwiftUI
struct FirstView: View {
#ObservedObject var firstCheck: ViewModel = ViewModel()
var body: some View {
VStack{
if firstCheck.check == true{
Text("checked")
}
}
}
}
struct FirstView_Previews: PreviewProvider {
static var previews: some View {
FirstView()
}
}
SecondView.swift
import SwiftUI
struct SecondView: View {
#ObservedObject var secondCheck = ViewModel()
var body: some View {
VStack{
Toggle(
isOn: $secondCheck.check
){
Text("change")
}
if self.secondCheck.check == true{
Text("checked")
}
}
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView()
}
}
ViewModel.swift
import Foundation
final class ViewModel: ObservableObject {
#Published var check: Bool = false
}
Xcode: Version 11.7
Keep object in one place, can be parent view
struct ContentView: View {
#ObservedObject var viewModel = ViewModel()
// #StateObject var viewModel = ViewModel() // SwiftUI 2.0
var body: some View {
TabView {
// .. other code here
}
.environmentObject(viewModel) // << inject here
}
}
and then use in both views like (for second the same)
struct FirstView: View {
#EnvironmentObject var firstCheck: ViewModel // declare only
// will be injected by type
var body: some View {
VStack{
if firstCheck.check == true{
Text("checked")
}
}
}
}

How to get a value as to which view is selected in TabView?

I'm currently developing an application using SwiftUI.
This app has 2 Views controlled a Tab View.
I want to get a value as to which view is selected in TabView.
Is there any way to do that?
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
FirstView.swift
import SwiftUI
struct FirstView: View {
var body: some View {
Text("FirstView")
}
}
struct FirstView_Previews: PreviewProvider {
static var previews: some View {
FirstView()
}
}
SecondView.swift
import SwiftUI
struct SecondView: View {
var body: some View {
Text("SecondView")
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView()
}
}
Xcode: Version 11.7
Swift: Swift 5
You need to use selection, like below
Note: selection should be same type as used for tags (and use corresponding values from tags to select specific tab programmatically)
struct ContentView: View {
#State private var selectedTab = 1 // default selection
var body: some View {
TabView(selection: $selectedTab) { // << here !!
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}