title: Apple TV - how to implement a sticky focus selection like in UISegmentedControl?
body: <p>In a custom control, how do you implement a sticky focus selection as seen in UISegmentedControl, UITabBar, UITableView, or UICollectionView? By 'sticky selection' I mean automatically diverting focus to the last focused/selected item when focusing into a container.</p>
  
  <p>How do UISegmentedControl + others implement this themselves? I tried inspecting UISegmentedControl and discovered to my surprise that it is a single large focusable element, not a group of focusable segments. I was not able to find out how UISegmentedControl is able to switch focus between segments given its architecture.</p>
  
  <p>So far the workaround I have is:</p>
  
  <p>At the screen level, maintain each sticky selection. For example if the screen has two custom top menus, the screen maintains two active selections. Override shouldUpdateFocus to detect focus changes into a container/menu with a sticky selection. Return false + call setNeedsFocusUpdate combined with overriding preferredFocusEnvironments to redirect focus to the sticky/selected subview instead.</p>
  
  <p>My solution works, but is unsatisfying for writing reusable custom UIs that maintain sticky selections automatically. Apple was able to make several such custom UIs, so there must be some method I am missing.</p>