Skip to content

Instantly share code, notes, and snippets.

@bachand
Created September 10, 2015 20:46
Show Gist options
  • Save bachand/364eb3add94b0de12318 to your computer and use it in GitHub Desktop.
Save bachand/364eb3add94b0de12318 to your computer and use it in GitHub Desktop.
Horizontally Centering Focused UIButton
class ActionHeroesView: UIView {
let stalloneButton: UIButton
let vanDammeButton: UIButton
// ...
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
// we'll center Stallone when neither is in focus since he's a bigger deal
let vanDammeFocused = (context.nextFocusedView == vanDammeButton)
layoutIfNeeded()
coordinator.addCoordinatedAnimations({
self.stalloneCenterXConstraint.active = !vanDammeFocused
self.vanDammeCenterXConstraint.active = vanDammeFocused
self.layoutIfNeeded()
},
completion: nil
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment