Dynamically Hiding View in Swiftui

Dynamically hiding view in SwiftUI

Rather than dynamically setting a variable and using it in my view, I found that I was able to hide or show the date picker this way:

struct ContentView : View {
@State var showDatePicker = true
@State var datePickerDate: Date = Date()

var body: some View {
VStack {
if self.showDatePicker {
DatePicker($datePickerDate)
} else {
DatePicker($datePickerDate).hidden()
}
}
}
}

Or, optionally, not including the date picker instead of hiding it:

struct ContentView : View {
@State var showDatePicker = true
@State var datePickerDate: Date = Date()

var body: some View {
VStack {
if self.showDatePicker {
DatePicker($datePickerDate)
}
}
}
}

(SwiftUI) How to make text hidden when view loads and become visible once toggle is tapped

You can continue to use a @State variable for showText, which, according to your requirements, should only be shown once the Toggle is interacted with. Rather than using a @State variable that is initialized with a UserDefaults value, you probably want to use @AppStorage for isOn.

struct ProfileView: View {

@AppStorage("isOn") private var isOn : Bool = false //@AppStorage syncs this value with UserDefaults automatically
@State private var showText = false

var body: some View {
VStack { // vstack 0
Text("Profile Page")
.font(.title)

Text("Are you ready to toggle?")
.font(.title2)
.multilineTextAlignment(.center)

VStack { // vstack 1
Toggle("toggle this", isOn: $isOn)
.onChange(of: isOn, perform: { value in
showText = true //Once the toggle is interacted with, set showText to true
})
if showText { //only show the text if showText == true
Text( isOn ? "Thank you." : "Toggled again" )
}
} // vstack 1
} // vstack 0
.padding(.bottom, 200) //try not to use UIScreen dimensions in SwiftUI -- default to using padding instead when possible
}
}

Hiding a SwiftUI View from another SwiftUI View

As Sam has stated already you could use a combination of @State and @Binding.

struct ContentView: View {

@State private var show = true

var body: some View {
VStack {
View1(show: $show)
View2(show: $show)
}
}
}

struct View1: View {

@Binding var show: Bool

var body: some View {
VStack {
if show {
Text("Text1")
}

Text("Text2")
}
}
}

struct View2: View {

@Binding var show: Bool

var body: some View {
VStack {
Text("Text3")

if show {
Text("Text4")
}

Button(action: {
self.show.toggle()
}) {
Text("Toogle show")
}
}
}
}

Showing and hiding views with transitions in swiftui

It looks like you forgot to put a . before transition(.asymmetric(insertion: .scale, removal: .opacity)).

That might be why.

Hide/show views on button tap - SwiftUI

You can show/hide your view in different ways. Here are some that may help you:

  1. opacity (the hidden view stays in the view hierarchy, ie. other views keep their positions):

    struct ContentView: View {
    @State var doIWantThisViewToShow: Bool = false

    var body: some View {
    VStack {
    Button("Show/Hide MyView") {
    doIWantThisViewToShow.toggle()
    }
    MyView()
    .padding()
    .opacity(doIWantThisViewToShow ? 1 : 0)
    }
    }
    }
  2. if-statement (the hidden view is removed from the view hierarchy, ie. other views positions will be rearranged):

    struct ContentView: View {
    @State var doIWantThisViewToShow: Bool = false

    var body: some View {
    VStack {
    Button("Show/Hide MyView") {
    doIWantThisViewToShow.toggle()
    }
    if doIWantThisViewToShow {
    MyView()
    .padding()
    }
    }
    }
    }

How do you make a Button conditionally hidden or disabled?

For me it worked perfectly to set the frame's height to zero when you do not want to see it. When you want to have the calculated size, just set it to nil:

SomeView
.frame(height: isVisible ? nil : 0)

If you want to disable it in addition to hiding it, you could set .disabled with the toggled boolean.

SomeView
.frame(height: isVisible ? nil : 0)
.disabled(!isVisible)


Related Topics



Leave a reply



Submit