@@ -36,8 +36,13 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
36
36
37
37
private weak var selectedContactInfoView : ContactInfoView !
38
38
private weak var addButtonRef : UIButton ! // reference to bottom AddButton
39
+ private weak var activeTextField : UITextField ?
39
40
private var contactInfoViewHeight : CGFloat = 0 // stores contact view height
40
41
42
+ deinit {
43
+ NSNotificationCenter . defaultCenter ( ) . removeObserver ( self )
44
+ }
45
+
41
46
override func viewDidLoad( ) {
42
47
super. viewDidLoad ( )
43
48
@@ -53,6 +58,7 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
53
58
}
54
59
55
60
contactEditScrollView. contentSize = contentRect. size
61
+ registerForKeyboardNotifications ( )
56
62
}
57
63
58
64
override func viewWillAppear( animated: Bool ) {
@@ -71,6 +77,36 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
71
77
self . navBar. doneButton. removeTarget ( self , action: " onDoneButtonClick " , forControlEvents: . TouchDown)
72
78
}
73
79
80
+ private func registerForKeyboardNotifications( ) {
81
+ NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " keyboardWasShown: " , name: UIKeyboardDidShowNotification, object: nil )
82
+ NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " keyboardWillBeHidden: " , name: UIKeyboardWillHideNotification, object: nil )
83
+ }
84
+
85
+ func keyboardWasShown( notification: NSNotification ) {
86
+ if activeTextField == nil {
87
+ return
88
+ }
89
+
90
+ let info = notification. userInfo
91
+ let kbSize = info![ UIKeyboardFrameBeginUserInfoKey] !. CGRectValue. size
92
+
93
+ let contentInsets = UIEdgeInsetsMake ( contactEditScrollView. contentInset. top, 0 , kbSize. height, 0 )
94
+ contactEditScrollView. contentInset = contentInsets
95
+ contactEditScrollView. scrollIndicatorInsets = contentInsets
96
+
97
+ var aRect = view. frame
98
+ aRect. size. height -= kbSize. height
99
+ if !CGRectContainsPoint( aRect, activeTextField!. frame. origin) {
100
+ contactEditScrollView. scrollRectToVisible ( activeTextField!. frame, animated: true )
101
+ }
102
+ }
103
+
104
+ func keyboardWillBeHidden( notification: NSNotification ) {
105
+ let contentInsets = UIEdgeInsetsMake ( contactEditScrollView. contentInset. top, 0 , 0 , 0 )
106
+ contactEditScrollView. contentInset = contentInsets
107
+ contactEditScrollView. scrollIndicatorInsets = contentInsets
108
+ }
109
+
74
110
func onDoneButtonClick( ) {
75
111
let firstNameOptional = textFields [ " First Name " ] ? . text
76
112
let lastNameOptional = textFields [ " Last Name " ] ? . text
@@ -127,6 +163,7 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
127
163
// build new view
128
164
let contactInfoView = ContactInfoView ( frame: CGRectMake ( 0 , y, contactEditScrollView. frame. size. width, 0 ) )
129
165
contactInfoView. delegate = self
166
+ contactInfoView. setTextFieldsDelegate ( self )
130
167
131
168
let record = ContactDetailRecord ( )
132
169
addedContactInfo. append ( record)
@@ -164,6 +201,14 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
164
201
return true
165
202
}
166
203
204
+ func textFieldDidBeginEditing( textField: UITextField ) {
205
+ activeTextField = textField
206
+ }
207
+
208
+ func textFieldDidEndEditing( textField: UITextField ) {
209
+ activeTextField = nil
210
+ }
211
+
167
212
// MARK: - ContactInfo delegate
168
213
func onContactTypeClick( view: ContactInfoView , withTypes types: [ String ] ) {
169
214
let picker = PickerSelector ( )
@@ -218,6 +263,7 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
218
263
let y = CGRectGetMaxY ( contactEditScrollView. subviews. last!. frame)
219
264
let contactInfoView = ContactInfoView ( frame: CGRectMake ( 0 , y, view. frame. size. width, 40 ) )
220
265
contactInfoView. delegate = self
266
+ contactInfoView. setTextFieldsDelegate ( self )
221
267
222
268
contactInfoView. record = record
223
269
contactInfoView. updateFields ( )
0 commit comments