-
Notifications
You must be signed in to change notification settings - Fork 912
/
ViewController.swift
90 lines (68 loc) · 2.88 KB
/
ViewController.swift
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
import UIKit
class ViewController: UIViewController {
@IBOutlet var scrollView : UIScrollView!
// var fr : UIView?
var oldContentInset = UIEdgeInsets.zero
var oldIndicatorInset = UIEdgeInsets.zero
var oldOffset = CGPoint.zero
var keyboardShowing = false
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name: .UIKeyboardWillHide, object: nil)
// content view's width and height constraints in storyboard are placeholders
let contentView = self.scrollView.subviews[0]
NSLayoutConstraint.activate([
contentView.widthAnchor.constraint(equalTo:self.scrollView.widthAnchor),
contentView.heightAnchor.constraint(equalTo:self.scrollView.heightAnchor),
])
self.scrollView.keyboardDismissMode = .interactive
}
// func textFieldDidBeginEditing(tf: UITextField) {
// self.fr = tf // keep track of first responder
// }
func textFieldShouldReturn(_ tf: UITextField) -> Bool {
print("return")
// self.fr = nil
tf.resignFirstResponder()
return true
}
override var shouldAutorotate : Bool {
return !self.keyboardShowing
}
func keyboardShow(_ n:Notification) {
if self.keyboardShowing {
return
}
self.keyboardShowing = true
print("show")
self.oldContentInset = self.scrollView.contentInset
self.oldIndicatorInset = self.scrollView.scrollIndicatorInsets
self.oldOffset = self.scrollView.contentOffset
let d = n.userInfo!
var r = d[UIKeyboardFrameEndUserInfoKey] as! CGRect
r = self.scrollView.convert(r, from:nil)
// no need to scroll, as the scroll view will do it for us
// so all we have to do is adjust the inset
self.scrollView.contentInset.bottom = r.size.height
self.scrollView.scrollIndicatorInsets.bottom = r.size.height
}
func keyboardHide(_ n:Notification) {
if !self.keyboardShowing {
return
}
self.keyboardShowing = false
print("hide")
// do { // work around bug
// let d = n.userInfo!
// let beginning = d[UIKeyboardFrameBeginUserInfoKey] as! CGRect
// let ending = d[UIKeyboardFrameEndUserInfoKey] as! CGRect
// if beginning == ending {print("bail!"); return}
// }
self.scrollView.contentOffset = self.oldOffset
self.scrollView.scrollIndicatorInsets = self.oldIndicatorInset
self.scrollView.contentInset = self.oldContentInset
// self.fr?.resignFirstResponder()
// self.fr = nil
}
}