Mac Catalyst SwiftUI – . focused() not working

Hello,

given this following simple SwiftUI setup:

struct ContentView: View {
    var body: some View {
        CustomFocusView()
    }
}

struct CustomFocusView: View {
    @FocusState private var isFocused: Bool
    
    var body: some View {
        color
            .frame(width: 128, height: 128)
            .focusable(true)
            .focused($isFocused)
            .onTapGesture {
                isFocused.toggle()
            }
            .onKeyPress("a") {
                print("A pressed")
                return .handled
            }
    }
    
    var color: Color {
        isFocused ? .blue : .red
    }
}

If I run this via Mac – Designed for iPad, the CustomFocusView toggles focus as expected and cycles through red and blue. Now if I run this same exact code via Mac Catalyst absolutely nothing happens and so far I wasn't able to ever get this view to accept focused state. Is this expected? I would appreciate if anyone could hint me on how to get this working.

Thank and best regards!

Anyone? This is one of the last steps of polish of my Catalyst app that I would like to implement, but I just don't see what's going wrong.

Based on these docs: https://developer.apple.com/documentation/swiftui/view/focusable(_:interactions:)

The focus interactions allowed for custom views changed in macOS 14—previously, custom views could only become focused with keyboard navigation enabled system-wide. Clients built using older SDKs will continue to see the older focus behavior, while custom views in clients built using macOS 14 or later will always be focusable unless the client requests otherwise by specifying a restricted set of focus interactions.

It reads to me like this should be possible with macOS 14 or later? Essentially what I want is similar to the behavior in Reality Composer Pro where when the user clicks the scene view keyboard controls are enabled to move around in the scene, whereas when focus changes e.g to elements on the sidebar keyboard navigation will cycle between them.

What works in my example above is using the arrow keys right away to focus the way. Although then I only get the focus ring, but the isFocused FocusState still does not update. Very confusing.

This video also didn't help much: https://developer.apple.com/documentation/swiftui/focus-cookbook-sample

Is this just somewhat unfinished behavior specific to Mac Catalyst?

Hi @arthurfromberlin ,

You are more than welcome to file an enhancement report regarding the focus not working on a color, but it does work on any view with an input (ie: TextField, Button, etc).

Try it out with a TextField instead and see if your code still works, in my tests it does.

Hi @Vision Pro Engineer and thanks for the reply! I don't necessarily need this to work on color. That was just an example to make it easily reproducible. My usecase is basically: I have a view with a sidebar on the left that contains items which should be keyboard navigatable. e.g list cells which can get focus by moving up and down and then make them listen for keypress. E.g I could press backspace to delete them or enter to make them renamable (turn the name label into a textfield). On the right side of the sidebar is a RealityKit scene view with custom camera controls. The idea would be that if the user puts focus on the right, or "clicks/taps" the scene view it becomes focused and then accepts keyboard input via onKeyPress. But this currently does not seem possible with what SwiftUI offers on Mac Catalyst? So should I rather roll my own focus management? I'm using a mix of UIKit and SwiftUI and currently retrieve keypresses on a container UIViewController via pressesBegan.

Hi @arthurfromberlin ,

Thanks so much for the extra info. This specific use case is a known issue, but I'd really appreciate it if you could file a bug at https://feedbackassistant.apple.com and post the FB number here so that I can forward it to the engineers. The more reports that we have, the better!!

In the meantime, I'll look around for a workaround.

Mac Catalyst SwiftUI – . focused() not working
 
 
Q