Refresh SwiftUI View Programmatically

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ม.ค. 2025

ความคิดเห็น • 16

  • @moiCode429
    @moiCode429 10 หลายเดือนก่อน +1

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

  • @rahuljamba5846
    @rahuljamba5846 10 หลายเดือนก่อน +1

    Thanks for sharing your knowledge !! keep it up

  • @30guarino
    @30guarino 10 หลายเดือนก่อน +1

    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

    • @FloWritesCode
      @FloWritesCode  10 หลายเดือนก่อน

      You should be able to use ScrollViewReader to scroll programmatically - I have a video on that API.

  • @marceljaeger
    @marceljaeger 10 หลายเดือนก่อน +1

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

  • @tylerwatt5651
    @tylerwatt5651 10 หลายเดือนก่อน

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

    • @FloWritesCode
      @FloWritesCode  10 หลายเดือนก่อน

      Yup, depending on your use case.

  • @markaurelius61
    @markaurelius61 10 หลายเดือนก่อน

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

    • @FloWritesCode
      @FloWritesCode  10 หลายเดือนก่อน +1

      Yup, playing around with the position of the id modifier probably makes sense 👍

  • @0xLarx
    @0xLarx 10 หลายเดือนก่อน +1

    Where are you mate 😂 last video 3 months ago

    • @FloWritesCode
      @FloWritesCode  10 หลายเดือนก่อน

      Been busy with real life stuff! Is there anything you'd like to see on the channel after this break? Thanks for sticking around!

  • @HomesteadAce
    @HomesteadAce 10 หลายเดือนก่อน

    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?

    • @markaurelius61
      @markaurelius61 10 หลายเดือนก่อน +1

      If internalState is not declared with @State, you can't change it in the button event

    • @SHOLINGER
      @SHOLINGER 10 หลายเดือนก่อน +2

      @HomesteadAce
      no , the whole point , because the struct is immutable , so @state property wrapper is definitely needed here !!