Created
September 10, 2015 20:46
-
-
Save bachand/364eb3add94b0de12318 to your computer and use it in GitHub Desktop.
Horizontally Centering Focused UIButton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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