Skip to content

Instantly share code, notes, and snippets.

@BradB132
Last active January 18, 2016 17:01
Show Gist options
  • Save BradB132/ebfff987cea256f444ff to your computer and use it in GitHub Desktop.
Save BradB132/ebfff987cea256f444ff to your computer and use it in GitHub Desktop.
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