Skip to content

Commit eecfb02

Browse files
committed
scrollview auto-scrolling added when the keyboard appears
1 parent c182b4a commit eecfb02

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

SampleAppSwift/SampleAppSwift/Controller/ContactEditViewController.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
3636

3737
private weak var selectedContactInfoView: ContactInfoView!
3838
private weak var addButtonRef: UIButton! // reference to bottom AddButton
39+
private weak var activeTextField: UITextField?
3940
private var contactInfoViewHeight: CGFloat = 0 // stores contact view height
4041

42+
deinit {
43+
NSNotificationCenter.defaultCenter().removeObserver(self)
44+
}
45+
4146
override func viewDidLoad() {
4247
super.viewDidLoad()
4348

@@ -53,6 +58,7 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
5358
}
5459

5560
contactEditScrollView.contentSize = contentRect.size
61+
registerForKeyboardNotifications()
5662
}
5763

5864
override func viewWillAppear(animated: Bool) {
@@ -71,6 +77,36 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
7177
self.navBar.doneButton.removeTarget(self, action: "onDoneButtonClick", forControlEvents: .TouchDown)
7278
}
7379

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+
74110
func onDoneButtonClick() {
75111
let firstNameOptional = textFields["First Name"]?.text
76112
let lastNameOptional = textFields["Last Name"]?.text
@@ -127,6 +163,7 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
127163
// build new view
128164
let contactInfoView = ContactInfoView(frame: CGRectMake(0, y, contactEditScrollView.frame.size.width, 0))
129165
contactInfoView.delegate = self
166+
contactInfoView.setTextFieldsDelegate(self)
130167

131168
let record = ContactDetailRecord()
132169
addedContactInfo.append(record)
@@ -164,6 +201,14 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
164201
return true
165202
}
166203

204+
func textFieldDidBeginEditing(textField: UITextField) {
205+
activeTextField = textField
206+
}
207+
208+
func textFieldDidEndEditing(textField: UITextField) {
209+
activeTextField = nil
210+
}
211+
167212
// MARK: - ContactInfo delegate
168213
func onContactTypeClick(view: ContactInfoView, withTypes types: [String]) {
169214
let picker = PickerSelector()
@@ -218,6 +263,7 @@ class ContactEditViewController: UIViewController, ProfileImagePickerDelegate, U
218263
let y = CGRectGetMaxY(contactEditScrollView.subviews.last!.frame)
219264
let contactInfoView = ContactInfoView(frame: CGRectMake(0, y, view.frame.size.width, 40))
220265
contactInfoView.delegate = self
266+
contactInfoView.setTextFieldsDelegate(self)
221267

222268
contactInfoView.record = record
223269
contactInfoView.updateFields()

SampleAppSwift/SampleAppSwift/View/ContactInfoView.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ class ContactInfoView: UIView, UITextFieldDelegate {
6868
*/
6969
func validateInfoWithResult(result: (Bool, String?) -> Void) {
7070
let email = textValueForKey("Email")
71-
if email.isEmpty || email.isValidEmail() {
72-
return result(true, nil)
73-
} else {
71+
if !email.isEmpty && !email.isValidEmail() {
7472
return result(false, "Not a valid email")
7573
}
74+
75+
return result(true, nil)
7676
}
7777

7878
func buildToDiciontary() -> [String: AnyObject] {
@@ -125,7 +125,6 @@ class ContactInfoView: UIView, UITextFieldDelegate {
125125
textField.font = UIFont(name: "Helvetica Neue", size: 20.0)
126126
textField.backgroundColor = UIColor.whiteColor()
127127
textField.layer.cornerRadius = 5
128-
textField.delegate = self
129128
addSubview(textField)
130129

131130
textFields[field] = textField
@@ -148,10 +147,9 @@ class ContactInfoView: UIView, UITextFieldDelegate {
148147
delegate.onContactTypeClick(self, withTypes: self.contactTypes)
149148
}
150149

151-
//MARK: - Text field delegate
152-
153-
func textFieldShouldReturn(textField: UITextField) -> Bool {
154-
textField.resignFirstResponder()
155-
return true
150+
func setTextFieldsDelegate(delegate: UITextFieldDelegate) {
151+
for textField in textFields.values {
152+
textField.delegate = delegate
153+
}
156154
}
157155
}

0 commit comments

Comments
 (0)