问题
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