Created
July 31, 2018 19:19
-
-
Save s4cha/34c41648a5cddf7264c9519f255f7301 to your computer and use it in GitHub Desktop.
Typical Stevia/ViewController use case
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
// | |
// ViewController.swift | |
// steviademo | |
// | |
// Created by Onur Geneş on 17.07.2018. | |
// Copyright © 2018 Onur Geneş. All rights reserved. | |
// | |
import UIKit | |
import Stevia | |
class ViewController: UIViewController { | |
let v = View() | |
override func loadView() { view = v } | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
v.textField.delegate = self | |
// Plug tap events. | |
v.addButton.addTarget(self, action: #selector(handleKeyboard), for: .touchUpInside) | |
v.sendButton.addTarget(self, action: #selector(handleKeyboardDismiss), for: .touchUpInside) | |
// Register for keyboard events. | |
let nc = NotificationCenter.default | |
nc.addObserver(self, selector: #selector(keyboardWillShowAnimation), name: .UIKeyboardWillShow, object: nil) | |
nc.addObserver(self, selector: #selector(keyboardWillHideAnimation), name: .UIKeyboardWillHide, object: nil) | |
} | |
@objc | |
func handleKeyboard() { | |
v.textField.becomeFirstResponder() | |
} | |
@objc | |
func handleKeyboardDismiss() { | |
v.textField.resignFirstResponder() | |
} | |
// MARK - Keyboard management | |
@objc | |
func keyboardWillShowAnimation(notification: Notification) { | |
guard let keyboardRect = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } | |
let collectionViewHeight = v.collectionView.frame.height | |
animateBottomViewWith(offset: keyboardRect.height - collectionViewHeight ) | |
} | |
@objc | |
func keyboardWillHideAnimation(notification: Notification) { | |
animateBottomViewWith(offset: 0) | |
} | |
private func animateBottomViewWith(offset: CGFloat) { | |
v.bottomView.bottomConstraint?.constant = offset | |
UIView.animate(withDuration: 0.5) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
} | |
extension ViewController: UITextFieldDelegate { | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
view.endEditing(true) | |
return true | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment