Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): auto layout with snapkit, form slide if keyboard ✨
- Loading branch information
1 parent
41a509d
commit 6141720
Showing
5 changed files
with
171 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
waosSwift/lib/helpers/Extensions/KeyboardConstraints.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// KeyboardConstraints.swift | ||
// snapkitkeyboard | ||
// | ||
// Created by Szymon Maślanka on 03/01/2017. | ||
// Copyright © 2017 Szymon Maślanka. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
extension ConstraintMakerEditable { | ||
@discardableResult | ||
func keyboard(_ shown: Bool, in view: UIView) -> ConstraintMakerEditable { | ||
switch view.traitCollection.verticalSizeClass { | ||
case .regular: | ||
if shown { view.shownRegularConstraints.append(constraint) } else { view.hiddenRegularConstraints.append(constraint) } | ||
case .compact: | ||
if shown { view.shownCompactConstraints.append(constraint) } else { view.hiddenCompactConstraints.append(constraint) } | ||
case .unspecified: break | ||
} | ||
return self | ||
} | ||
} | ||
|
||
private var ckmShownRegular: UInt8 = 0 | ||
private var ckmShownCompact: UInt8 = 1 | ||
private var ckmHiddenRegular: UInt8 = 2 | ||
private var ckmHiddenCompact: UInt8 = 3 | ||
extension UIView { | ||
|
||
fileprivate var shownRegularConstraints: [Constraint]! { | ||
get { return objc_getAssociatedObject(self, &ckmShownRegular) as? [Constraint] ?? [Constraint]() } | ||
set(newValue) { objc_setAssociatedObject(self, &ckmShownRegular, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } | ||
} | ||
|
||
fileprivate var shownCompactConstraints: [Constraint]! { | ||
get { return objc_getAssociatedObject(self, &ckmShownCompact) as? [Constraint] ?? [Constraint]() } | ||
set(newValue) { objc_setAssociatedObject(self, &ckmShownCompact, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } | ||
} | ||
|
||
fileprivate var hiddenRegularConstraints: [Constraint]! { | ||
get { return objc_getAssociatedObject(self, &ckmHiddenRegular) as? [Constraint] ?? [Constraint]() } | ||
set(newValue) { objc_setAssociatedObject(self, &ckmHiddenRegular, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } | ||
} | ||
|
||
fileprivate var hiddenCompactConstraints: [Constraint]! { | ||
get { return objc_getAssociatedObject(self, &ckmHiddenCompact) as? [Constraint] ?? [Constraint]() } | ||
set(newValue) { objc_setAssociatedObject(self, &ckmHiddenCompact, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } | ||
} | ||
|
||
@objc private dynamic func keyboardWillShow(notification: Notification) { | ||
let duration = (notification as NSNotification).userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval | ||
let option = (notification as NSNotification).userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber | ||
|
||
switch traitCollection.verticalSizeClass { | ||
case .regular: | ||
hiddenRegularConstraints.forEach { $0.deactivate() } | ||
shownRegularConstraints.forEach { $0.activate() } | ||
case .compact: | ||
hiddenCompactConstraints.forEach { $0.deactivate() } | ||
shownCompactConstraints.forEach { $0.activate() } | ||
case .unspecified: break | ||
} | ||
|
||
UIView.animate(withDuration: duration, delay: 0, | ||
options: UIView.AnimationOptions(rawValue: option.uintValue), animations: { | ||
self.layoutIfNeeded() | ||
}, completion: nil) | ||
} | ||
|
||
@objc private dynamic func keyboardWillHide(notification: Notification) { | ||
let duration = (notification as NSNotification).userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval | ||
let option = (notification as NSNotification).userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber | ||
|
||
switch traitCollection.verticalSizeClass { | ||
case .regular: | ||
shownRegularConstraints.forEach { $0.deactivate() } | ||
hiddenRegularConstraints.forEach { $0.activate() } | ||
case .compact: | ||
shownCompactConstraints.forEach { $0.deactivate() } | ||
hiddenCompactConstraints.forEach { $0.activate() } | ||
case .unspecified: break | ||
} | ||
|
||
UIView.animate(withDuration: duration, delay: 0, | ||
options: UIView.AnimationOptions(rawValue: option.uintValue), animations: { | ||
self.layoutIfNeeded() | ||
}, completion: nil) | ||
} | ||
|
||
func registerAutomaticKeyboardConstraints() { | ||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil) | ||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) | ||
} | ||
} |
This file contains 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
This file contains 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