3

I'm having this issue with SwiftUI on Mac Catalyst wherein a simple view like the following :

struct ContentView: View {
    @State var count : Int = 0
    
    var body: some View {
        HStack{
            Button("tap me"){
                count += 1
            }
            Button("test \(count)"){
                count += 1
            }.buttonStyle(CustomButtonStyle())
            Button("test \(count)"){
                count += 1
            }.buttonStyle(CustomButtonStyle())
            Button("test \(count)"){
                count += 1
            }.buttonStyle(CustomButtonStyle())
        }
    }
}

struct CustomButtonStyle: ButtonStyle {
    
    func makeBody(configuration: Configuration) -> some View {
        configuration
            .label
    }
}

The button with no style (but also everything else from pickers to sliders) becomes unresponsive after a number of renders. This happens only when two or more Buttons with custom style are visible on the screen. Having different styles doesn't solve the issue. Have you encountered this issue before? Is it a bug with SwiftUI on mac?

4
  • I've tried your code and all is working well for me on mac 11.3-beta, using xcode 12.5-beta, targets ios 14.x, mac catalyst 11.3. Mar 24, 2021 at 7:59
  • that's interesting. To replicate the issue you should have pressed repeatedly on all three buttons with custom style at least once. Have you done that? Thanks for trying @workingdog Mar 24, 2021 at 21:18
  • yes done that, clicked the buttons many many times. Tried different sequences as well, and all is still working well. Mar 24, 2021 at 22:47
  • FWIW, I also checked (since I'm having similar non-responsiveness-after-a-while issues with TextField), but it all works for me and does not become unresponsive. XCode 12.4. Mar 31, 2021 at 16:52

1 Answer 1

3

It turns out this problem is reproducible when deploying using "Optimize Interface for Mac". If you use "Scale Interface to Match iPad", it works without a problem. Specifying a contentShape fixes the problem for some reason. FWIW, just specifying the contentShape for the non-custom (i.e., native when using "Optimize Interface for Mac") works in a VStack, but not in an HStack.

struct ContentView: View {
    @State private var count: Int = 0
    
    var body: some View {
        HStack {
            Button("tap me") { count += 1 }
            .contentShape(Rectangle())
            Button("test \(count)") { count += 1 }
            .contentShape(Rectangle())
            .buttonStyle(CustomButtonStyle())
            Button("test \(count)") { count += 1 }
            .contentShape(Rectangle())
            .buttonStyle(CustomButtonStyle())
            Button("test \(count)") { count += 1 }
            .contentShape(Rectangle())
            .buttonStyle(CustomButtonStyle())
        }
    }
    
}

struct CustomButtonStyle: ButtonStyle {
    
    func makeBody(configuration: Configuration) -> some View {
        configuration
            .label
    }
    
}
3
  • Yes! I missed to mention that I'm building an app for Mac and iOS so that 'optimize for Mac ' option is enabled for me. May 5, 2021 at 3:36
  • It seems that specifying the contentShape fixes the problem, altho I don't really understand what's going on behind the scenes with SwiftUI in "Optimize Interface for Mac" that makes this so. It caused me problems when using Button along with TextField. Providing code above. May 8, 2021 at 0:21
  • 1
    In hopes it might one day be fixed, I filed FB9101621 May 10, 2021 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.