Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Focus_in_tvOS
import UIKit
class ViewController: UIViewController {
@IBOutlet var buttonA:UIButton!
@IBOutlet var buttonB:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let focusGuide = UIFocusGuide()
focusGuide.preferredFocusedView = buttonB
view.addLayoutGuide(focusGuide)
view.addConstraint(NSLayoutConstraint(item: focusGuide,
attribute: .Width,
relatedBy: .Equal,
toItem: buttonA,
attribute: .Width,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: focusGuide,
attribute: .Height,
relatedBy: .Equal,
toItem: buttonA,
attribute: .Height,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: focusGuide,
attribute: .CenterX,
relatedBy: .Equal,
toItem: buttonB,
attribute: .CenterX,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: focusGuide,
attribute: .CenterY,
relatedBy: .Equal,
toItem: buttonA,
attribute: .CenterY,
multiplier: 1,
constant: 0))
}
//MARK: - UIFocusEnvironment
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
//TODO: update the 'preferredFocusView' of our focusGuide depending on which view is now in focus
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment