Articles and News about Mobile Development and Software Development in
general, by Prajeet Shrestha
Focus on custom UI element on tvOS
10 Jul 2024
Working with prebuilt SwiftUI element like button and image in tvOS is easy but when it comes to customization, it
can be a bit tricky.
In this article, we will focus on how to customize the focus on custom UI elements in tvOS.
Please note that order of modifiers used is very important while customizing the focus on custom UI elements. If you
messup the order of the modifiers, the focus might not work as expected.
I will be using the following code snippet to demonstrate the focus on custom UI elements in tvOS.
Important bits of code
Custom hashable node that can be used as focus state
Focus State
Displays the name of the person that is focused
VStack is important to wrap ListItem so that the isFocused environment variable is triggered when ListItem
is triggered.
After the view is made focusable, you can use the .focused view modifier on it to bind with the focus state
On tap gesture is added only after the focusable view modifier is added
Returns whether the nearest focusable ancestor has focus.