Are there any hidden tools in tvOS for Focus Engine debugging?

老子叫甜甜 提交于 2019-12-03 13:43:20

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):

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!