-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardLayoutGuideView.swift
More file actions
111 lines (86 loc) · 3.64 KB
/
KeyboardLayoutGuideView.swift
File metadata and controls
111 lines (86 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// KeyboardLayoutGuideView.swift
//
// Created by Thomas Sempf on 2015-06-12.
// Copyright © 2015 BitWizardry. All rights reserved.
//
import Foundation
import UIKit
class KeyboardLayoutGuideView: UIView {
private weak var bottomLayoutGuideConstraint: NSLayoutConstraint?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Do any additional setup after loading the view, typically from a nib.
setupObservers()
translatesAutoresizingMaskIntoConstraints = false
}
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
setupObservers()
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func updateConstraints() {
defer { super.updateConstraints() }
guard let superview = self.superview else { return }
if constraints.count == 0 {
setupLeftRightLayoutConstraintsWithSuperview(superview)
}
if bottomLayoutGuideConstraint == nil {
setupBottomLayoutConstraintWithSuperview(superview)
}
}
}
private extension KeyboardLayoutGuideView {
func setupObservers() {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "showKeyboardNotification:",
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "hideKeyboardNotification:",
name: UIKeyboardWillHideNotification,
object: nil)
}
func setupLeftRightLayoutConstraintsWithSuperview(superview: UIView) {
superview.leadingAnchor.constraintEqualToAnchor(self.leadingAnchor, constant: 0).active = true
superview.trailingAnchor.constraintEqualToAnchor(self.trailingAnchor, constant: 0).active = true
heightAnchor.constraintEqualToConstant(0).active = true
}
func setupBottomLayoutConstraintWithSuperview(superview: UIView) {
if let existingBottomLayoutConstraint = findExistingBottomLayoutConstraintWithSuperview(superview) {
superview.removeConstraint(existingBottomLayoutConstraint)
}
bottomLayoutGuideConstraint = superview.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: 0)
bottomLayoutGuideConstraint?.active = true
}
func findExistingBottomLayoutConstraintWithSuperview(superview: UIView) -> NSLayoutConstraint? {
return superview.constraints.filter({ constraint -> Bool in
if constraint.firstAttribute == .Bottom && constraint.secondAttribute == .Bottom &&
(constraint.firstItem === self || constraint.secondItem === self)
{
return true
}
return false
}).first
}
dynamic func showKeyboardNotification(notification: NSNotification) {
guard let
userInfo = notification.userInfo,
endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
else { return }
self.superview?.setNeedsLayout()
self.bottomLayoutGuideConstraint?.constant = endFrame.height
UIView.animateWithDuration(0.5) { () -> Void in
self.superview?.layoutIfNeeded()
}
}
dynamic func hideKeyboardNotification(notification: NSNotification) {
self.bottomLayoutGuideConstraint?.constant = 0
UIView.animateWithDuration(0.5) { () -> Void in
self.superview?.layoutIfNeeded()
}
}
}