@@ -9,12 +9,13 @@ func lend<T> (closure:(T)->()) -> T where T:NSObject {
9
9
10
10
class ViewController : UIViewController , UITextViewDelegate {
11
11
@IBOutlet var tv : UITextView !
12
- var scrollView : UIScrollView ! {
12
+ var sv : UIScrollView ! {
13
13
return self . tv
14
14
}
15
15
var oldContentInset = UIEdgeInsets . zero
16
16
var oldIndicatorInset = UIEdgeInsets . zero
17
17
var oldOffset = CGPoint . zero
18
+ var prevr = CGRect . zero
18
19
19
20
override func viewDidLoad( ) {
20
21
super. viewDidLoad ( )
@@ -72,42 +73,48 @@ class ViewController: UIViewController, UITextViewDelegate {
72
73
ks = . exiting
73
74
}
74
75
}
76
+ print ( ks == . entering ? " entering " : ks == . exiting ? " exiting " : " unknown " )
75
77
return ( ks, newRect)
76
78
}
77
-
78
- #error("needs updating")
79
+
79
80
@objc func keyboardShow( _ n: Notification ) {
81
+ print ( " show " )
82
+ guard self . traitCollection. userInterfaceIdiom == . phone else { return }
80
83
let d = n. userInfo!
81
- let ( state, rnew) = keyboardState ( for: d, in: self . scrollView )
84
+ let ( state, rnew) = keyboardState ( for: d, in: self . sv )
82
85
if state == . entering {
83
86
print ( " really showing " )
84
- self . oldContentInset = self . scrollView . contentInset
85
- self . oldIndicatorInset = self . scrollView . scrollIndicatorInsets
86
- self . oldOffset = self . scrollView . contentOffset
87
+ self . oldContentInset = self . sv . contentInset
88
+ self . oldIndicatorInset = self . sv . verticalScrollIndicatorInsets
89
+ self . oldOffset = self . sv . contentOffset
87
90
}
88
- print ( " show " )
89
91
// no need to scroll, as the scroll view will do it for us
90
92
// so all we have to do is adjust the inset
91
- if let rnew = rnew {
92
- let h = rnew. intersection ( self . scrollView. bounds) . height
93
- self . scrollView. contentInset. bottom = h
94
- self . scrollView. scrollIndicatorInsets. bottom = h
93
+ if let rnew = rnew, rnew != self . prevr {
94
+ print ( " adjusting " )
95
+ self . prevr = rnew
96
+ let h = rnew. intersection ( self . sv. bounds) . height + 6 // ?
97
+ self . sv. contentInset. bottom = h
98
+ self . sv. verticalScrollIndicatorInsets. bottom = h
95
99
}
96
100
}
97
101
98
102
@objc func keyboardHide( _ n: Notification ) {
103
+ print ( " hide " )
104
+ guard self . traitCollection. userInterfaceIdiom == . phone else { return }
99
105
let d = n. userInfo!
100
- let ( state, _) = keyboardState ( for: d, in: self . scrollView )
106
+ let ( state, _) = keyboardState ( for: d, in: self . sv )
101
107
if state == . exiting {
102
108
print ( " really hiding " )
103
109
// restore original setup
104
- // we _don't_ do this; let the text view position itself
105
- // self.scrollView.contentOffset = self.oldOffset
106
- self . scrollView . scrollIndicatorInsets = self . oldIndicatorInset
107
- self . scrollView . contentInset = self . oldContentInset
110
+ self . sv . contentOffset = self . oldOffset
111
+ self . sv . verticalScrollIndicatorInsets = self . oldIndicatorInset
112
+ self . sv . contentInset = self . oldContentInset
113
+ self . prevr = . zero
108
114
}
109
115
}
110
116
117
+
111
118
112
119
113
120
@objc func doDone( _ sender: Any ) {
0 commit comments