Playgrounds App on iPad not updating UI on @State change

iPad Pro running iOS 14.4, Playgrounds App version 3.4

Super simple @State example doesn’t update the UI. Clicking the button increments the tapCount variable, but the UI doesn’t update to show the new value. Isn’t it supposed to?

Am I using an old code example or otherwise doing something wrong? This exact same code works fine on a Playground in Xcode.

Code Block Swift
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
    @State private var tapCount = 0
    var body: some View {
        Button("Tap count: \(tapCount)") {
            tapCount += 1
        }
    }
}
PlaygroundPage.current.setLiveView(ContentView())


Replies

Well, that’s interesting. I put your code into Playgrounds on the Mac (1) and it worked. However, the same code fails as you described in Playgrounds on my iPad. Weird, and definitely bugworthy.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"

(1) Not an Xcode playground, but the Mac Catalyst version of the iOS Playgrounds app.
I think this is the bug number: FB9018696
  • Did you solve this trouble? I have the same issue. Moreover all animations are working very strange. On starting animated view placed in center appearing in top-left corner and shifting to the center with animation.

Add a Comment

I’m seeing the same issue on my iPad Pro 11”

FB9121354: Swift Playgrounds on iPad does not update UI for @State variables

The code works on Mac, not on iPad Playgrounds.

import SwiftUI
import PlaygroundSupport

struct ContentView: View {

    @State var isSelected: Bool = true
    @State var useRedText = false

    var body: some View {

        VStack {

            Text("SwiftUI on iPad")

                .font(.headline)

                .padding(20)

                .background(Color.red)

                .cornerRadius(10)

            Text("Paul Rocks!")

                .font(.largeTitle)

                .padding(20)

                .background(isSelected ? Color.orange : Color.blue) //color())

                .cornerRadius(30)

                .onTapGesture { 

                    print("Paul Rocks")

                    isSelected.toggle()

                }

            

            Button("Hello World") {

                    // flip the Boolean between true and false

                    self.useRedText.toggle()      

                }

                .foregroundColor(useRedText ? .red : .blue)

        }

    }

    

    func color() -> Color {

        isSelected ? Color.orange : Color.blue

    }

}


PlaygroundPage.current.setLiveView(ContentView())

Just tested both codes (wrees3 and PaulSolt) on my iPad Air (2020) iOS 14.6, Playground App 3.4.1 (1302.34)

Both code don't work by default. But if I turn off "Enable Results" they both work perfect with no changes in code.

LumuS many thanks! So simply. Everything is work. May be you know how to switch it off by default?

  • Sorry. I don't know how to switch it off by default.

Add a Comment