Refresh SwiftUI View Programmatically

preview_player
Показать описание
Sometimes changing @.State isn't enough to refresh/update your Views in SwiftUI. Let's learn about the .id ViewModifier and how the SwiftUI layout system works.

Join this channel to get access to perks:

Affiliate Links ❤

If you have any video suggestions please feel free to let me know by a comment.
Get in contact via
Рекомендации по теме
Комментарии
Автор

thanks a lot, this really helps !! i was like looking for something to reset my view easily with timer

abdulrhmandakhel
Автор

Absolutely beautiful, this is what I needed to force refresh a view on my Apps Homepage and I was struggling really hard. Such a simple neat trick.

RahulGuptaRon
Автор

thank you bruh, you da best. Theese short videos me like su much ! 🔥

moiCode
Автор

Nice video and yeah sometimes you may need to update a view body programatically. But you need to be carefully with changing the identity of a view. When you change an identity of a view. The whole view hierarchy of that view will be created new and that can cause performance issues. Additionally the State properties of Subviews of the HStack will be created new, because the identity changed. So I think a better way may be this:

This causes an update of the body of ContentView but not for all Subviews of the HStack if the View values of those Subviews is not different.
An other way can be to change the identity of the Color in background without the If statement.

struct ContentView: View {
@State var value: Bool = true

var body: some View {
let _ = Self._printChanges()

HStack {
Button {
value.toggle()
} label: {
Text("Refresh")
}

//Other sub views
}
.background {
if isTrue {
Color.black.opacity(0)
}else {
Color.black.opacity(0)
}
}

}
}

marceljaeger
Автор

Flo, Was wondering if you can do a video or provide any documentation on how in a chat message view have the last message scroll up above the keyboard when it APPEARS?....Any help will be greatly appreciated

guarino
Автор

I'd assume you could use .onAppear in place of the button right?

tylerwatt
Автор

You can keep the code tighter by putting the id change after the button:
Button("Refresh") { (internalState += 1) }
.id(internalState)

markaurelius
visit shbcf.ru