Focus jumps to different view from one returned from preferredFocusEnvironment

Viewed 15

I have a a following view enter image description here

and here is preferredFocusEnvironment var code

override var preferredFocusEnvironments: [UIFocusEnvironment] {
        if let currentlyHighlightedButton = self.currentlyHighlightedButton {
            return [currentlyHighlightedButton]
        } else if let firstButton = buttonsStack.arrangedSubviews.first {
            return [firstButton]
        }
        return []
    }

when I step through with a debugger currentlyHighlightedButton is always returned and it's always "BROWSE"

But then focus jumps to "SEARCH" (button with magnifying glass icon)

Here's focus logging output right before the focus jumps:

The result of the focus update was determined from the following preferred focus search:
|
|   Starting preferred focus search:
|   |--> Searching <UIFocusSystem 0x281320f20>...
|        |--> Searching <_UIFocusSystemSceneComponent 0x280f28310>...
|             |--> Searching <UIWindow 0x104e1e2a0>...
|                  |--> Searching <TopNavigationBarContainer 0x104e43920>...
|                       |--> Searching <TopNavigationBar 0x107057400>...
|                            |--> Searching <TopNavigationBarButton 0x104d53150>... <- this is BROWSE 
|                                 No more preferences for this environment, and there are no focusable items in this environment to prefer by default.
|                            No more preferences for this environment, but we inferred a default preference (<TopNavigationBarButton 0x104d4e200>) from the visual layout for <TopNavigationBar 0x107057400>...
|                            |--> Searching <TopNavigationBarButton 0x104d4e200>... <- this is SEARCH
|                                 It's focusable!
|

Can't figure out what causes this jump

No more preferences for this environment, and there are no focusable items in this environment to prefer by default. No more preferences for this environment, but we inferred a default preference (<TopNavigationBarButton 0x104d4e200>) from the visual layout for <TopNavigationBar 0x107057400>...

I want BROWSE button to gain and maintain focus.

One peculiarity is when I put breakpoints in preferredFocusEnvironment and step through this jump does not happen

0 Answers
Related