Skip to content

Instantly share code, notes, and snippets.

@nickmain
Created January 11, 2022 00:29
Show Gist options
  • Save nickmain/5e2c6371dc2e2f7de525568a143776bc to your computer and use it in GitHub Desktop.
Save nickmain/5e2c6371dc2e2f7de525568a143776bc to your computer and use it in GitHub Desktop.
Forced focus targetting
// Focus management
private var targetFocusEnv: UIFocusEnvironment?
override var preferredFocusEnvironments: [UIFocusEnvironment] {
if let target = targetFocusEnv {
return [target]
} else {
return []
}
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
// clear focus target
targetFocusEnv = nil
}
func tryToSendFocus(to environment: UIFocusEnvironment) {
runOnMain {
self.targetFocusEnv = environment
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment