UIButton on different Y level can't be focused on tvOS

守給你的承諾、 提交于 2020-01-02 01:19:26

问题


I started developing apps for Apple TV and I have this problem whit buttons. I have a screen where are several buttons on bottom of the screen and then there is one, which is in the middle (see screenshot to understand). The problem is that I can't get focused the middle button. Focus is only on bottom three buttons. The problem is obviously caused by the Y position, when I move the button on bottom of the screen, it gets focused.

Buttons are created in storyboard, there is no code in ViewController. Settings in storyboard for the middle button are equal with settings for bottom buttons. Do I have to handle this manually in code? Or is it even possible to get focus movement like this? I would appreciate any information, thanks.

I'm still developing in simulator.


回答1:


So I was guided by apple to the solution. It requires a little coding using UIFocusGuide. Apple has a sample code for this.




回答2:


Add a focus guide to fill in the missing space.

    let focusGuide = UIFocusGuide()
    view.addLayoutGuide(focusGuide)

    focusGuide.widthAnchor.constraintEqualToAnchor(buttonThatIsFocused.widthAnchor).active = true
    focusGuide.leftAnchor.constraintEqualToAnchor(buttonThatIsFocused.leftAnchor).active = true
    focusGuide.heightAnchor.constraintEqualToAnchor(buttonTryingToFocus).active = true

    focusGuide.preferredFocusedView = buttonTryingToFocus


来源:https://stackoverflow.com/questions/32759700/uibutton-on-different-y-level-cant-be-focused-on-tvos

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