preferredFocusEnvironments behavior is broken in child view controllers
Originator: | paul.von.schrottky | ||
Number: | rdar://17065509 | Date Originated: | |
Status: | Resolved: | ||
Product: | Product Version: | iOS 18.3.1 | |
Classification: | Bug | Reproducible: | Yes |
**Description:** When a view controller is embedded as a child in a container view controller, the focus system fails to properly traverse the `preferredFocusEnvironments` tree. The container view controller needs to explicitly pass through its wrapped child environment by overriding `preferredFocusEnvironments` to return its children. **Steps to Reproduce:** 1. Let's say `MyViewController` customizes the focus behavior its sub views (e.g. to reverse the focus order of a group of buttons) and this works as expected 2. If this view controller is now embedded in a container view controller, `WrapperViewController`, the customization logic (which lives in the overriden property `preferredFocusEnvironments` of `MyViewController`) is never called 3. The solution is to also override `preferredFocusEnvironments` in the container view controller: ``` class WrapperViewController: UIViewController { open override var preferredFocusEnvironments: [any UIFocusEnvironment] { return self.children // children contains a MyViewController instance } } ``` **Expected Results:** - Focus behavior should be consistent whether `MyViewController` is or is not a child view controller - Focus should follow the custom order defined in `preferredFocusEnvironments` **Actual Results:** - When MyViewController is not a child: `preferredFocusEnvironments` is called in MyViewController - When MyViewController is child: Focus system falls back to visual search order, ignoring MyViewController's `preferredFocusEnvironments` override - The fix requires explicitly overriding `preferredFocusEnvironments` in the container to return its children **Additional Info:** - Xcode 16.2 (16C5032a)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at feedbackassistant.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!