Are there any hidden tools in tvOS for Focus Engin

2019-03-27 14:59发布

问题:

One of my favorite hidden debugging tools for iOS is using recursiveDescription on UIView instances. This is very useful for troubleshooting view locations that may be off-screen, for example. Debugging the Focus Engine on tvOS brings its own set of challenges, especially around what it thinks are focusable elements.

Are there any hidden debugging tools for tvOS to introspect what is going on inside the Focus Engine?

回答1:

There are two helpful methods, both of which are actually documented in the App Programming Guide for tvOS.

UIView

If you're trying to move focus to a particular view and can't, there's a debugging method on UIView that can help explain why: _whyIsThisViewNotFocusable

The output from this method looks something like this:

(lldb) po [(UIView *)0x148db5234 _whyIsThisViewNotFocusable]
ISSUE: This view has userInteractionEnabled set to NO. Views must allow user interaction to be focusable.
ISSUE: This view returns NO from -canBecomeFocused.

UIFocusUpdateContext

The UIFocusUpdateContext object supports Xcode's QuickLook feature, so if you're paused in the debugger you can press spacebar (or click the eyeball icon next to the variable) to see a graphical representation of what the focus engine sees (image is from Apple's documentation):



回答2:

Since tvOS 11, troubleshooting unreachable items got a lot easier, just add this to the viewDidLoad of the ViewController in Question:

 if #available(tvOS 11.0, *) {
  NotificationCenter.default.addObserver(
    forName: NSNotification.Name.UIFocusMovementDidFail
  ) { [weak self] notification in
    let context = notification.userInfo![UIFocusUpdateContextKey] as! UIFocusUpdateContext
    print(context) // If you add a breakpoint here you can quicklook the context in the debugger for more information
    print(UIFocusDebugger.checkFocusability(for: self!.collectionView)) // replace collectionView with the view you want to check
  }
}

This results in debug output like:

<UIFocusUpdateContext: 0x6080000fe800: previouslyFocusedItem=<UIKeyboard 0x7fc597d75610>, nextFocusedItem=(null), focusHeading=Down>

The following issues were found that would prevent this item from being focusable:
- ISSUE: The item is being visually occluded by the following items:
<UIView 0x7fc597c3a9e0>