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
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) } } } }
Wasn't the whole point of this view to not use @state? You should have removed it at the end to show that the refresh happens because of the id and not the state change?
thank you bruh, you da best. Theese short videos me like su much ! 🔥
Thanks for sharing your knowledge !! keep it up
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
You should be able to use ScrollViewReader to scroll programmatically - I have a video on that API.
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)
}
}
}
}
I'd assume you could use .onAppear in place of the button right?
Yup, depending on your use case.
You can keep the code tighter by putting the id change after the button:
Button("Refresh") { (internalState += 1) }
.id(internalState)
Yup, playing around with the position of the id modifier probably makes sense 👍
Where are you mate 😂 last video 3 months ago
Been busy with real life stuff! Is there anything you'd like to see on the channel after this break? Thanks for sticking around!
Wasn't the whole point of this view to not use @state? You should have removed it at the end to show that the refresh happens because of the id and not the state change?
If internalState is not declared with @State, you can't change it in the button event
@HomesteadAce
no , the whole point , because the struct is immutable , so @state property wrapper is definitely needed here !!