Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add public method to adjust the scrollview when switching between tex…

…tFields
  • Loading branch information...
commit eaec6f0f269aa126ed49754603aa08f0f46f042a 1 parent e5dbefc
@wordofchristian wordofchristian authored
View
13 TPKeyboardAvoidingSample/FirstViewController.h
@@ -8,9 +8,22 @@
#import <UIKit/UIKit.h>
+@class TPKeyboardAvoidingScrollView;
@interface FirstViewController : UIViewController <UITextFieldDelegate> {
+ TPKeyboardAvoidingScrollView *scrollView;
+ UITextField *txtIggle;
+ UITextField *txtNiggle;
+ UITextField *txtOggle;
+ UITextField *txtBogle;
+ UITextField *txtSplat;
}
+@property (nonatomic, retain) IBOutlet TPKeyboardAvoidingScrollView *scrollView;
+@property (nonatomic, retain) IBOutlet UITextField *txtIggle;
+@property (nonatomic, retain) IBOutlet UITextField *txtNiggle;
+@property (nonatomic, retain) IBOutlet UITextField *txtOggle;
+@property (nonatomic, retain) IBOutlet UITextField *txtBogle;
+@property (nonatomic, retain) IBOutlet UITextField *txtSplat;
@end
View
46 TPKeyboardAvoidingSample/FirstViewController.m
@@ -7,9 +7,15 @@
//
#import "FirstViewController.h"
-
+#import "TPKeyboardAvoidingScrollView.h"
@implementation FirstViewController
+@synthesize scrollView;
+@synthesize txtIggle;
+@synthesize txtNiggle;
+@synthesize txtOggle;
+@synthesize txtBogle;
+@synthesize txtSplat;
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
@@ -36,6 +42,12 @@ - (void)didReceiveMemoryWarning
- (void)viewDidUnload
{
+ [self setScrollView:nil];
+ [self setTxtIggle:nil];
+ [self setTxtNiggle:nil];
+ [self setTxtOggle:nil];
+ [self setTxtBogle:nil];
+ [self setTxtSplat:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
@@ -45,12 +57,42 @@ - (void)viewDidUnload
- (void)dealloc
{
+ [scrollView release];
+ [txtIggle release];
+ [txtNiggle release];
+ [txtOggle release];
+ [txtBogle release];
+ [txtSplat release];
[super dealloc];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
+ if (textField == txtIggle) {
+ [txtNiggle becomeFirstResponder];
+ }
+
+ else if (textField == txtNiggle) {
+ [txtOggle becomeFirstResponder];
+ }
+
+ else if (textField == txtOggle) {
+ [txtBogle becomeFirstResponder];
+ }
+
+ else if (textField == txtBogle) {
+ [txtSplat becomeFirstResponder];
+ }
+ else{
+ [textField resignFirstResponder];
+ }
+
+
return YES;
}
+-(void)textFieldDidBeginEditing:(UITextField *)textField
+{
+ [scrollView adjustOffsetToIdealIfNeeded];
+}
+
@end
View
122 TPKeyboardAvoidingSample/en.lproj/FirstView.xib
@@ -2,10 +2,10 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
<int key="IBDocument.SystemTarget">1024</int>
- <string key="IBDocument.SystemVersion">10J567</string>
+ <string key="IBDocument.SystemVersion">10J869</string>
<string key="IBDocument.InterfaceBuilderVersion">1305</string>
<string key="IBDocument.AppKitVersion">1038.35</string>
- <string key="IBDocument.HIToolboxVersion">462.00</string>
+ <string key="IBDocument.HIToolboxVersion">461.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="NS.object.0">300</string>
@@ -73,7 +73,7 @@
<bool key="IBUIAdjustsFontSizeToFit">YES</bool>
<float key="IBUIMinimumFontSize">17</float>
<object class="IBUITextInputTraits" key="IBUITextInputTraits">
- <int key="IBUIReturnKeyType">9</int>
+ <int key="IBUIReturnKeyType">4</int>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
</object>
@@ -156,7 +156,7 @@
<bool key="IBUIAdjustsFontSizeToFit">YES</bool>
<float key="IBUIMinimumFontSize">17</float>
<object class="IBUITextInputTraits" key="IBUITextInputTraits">
- <int key="IBUIReturnKeyType">9</int>
+ <int key="IBUIReturnKeyType">4</int>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
</object>
@@ -182,7 +182,7 @@
<bool key="IBUIAdjustsFontSizeToFit">YES</bool>
<float key="IBUIMinimumFontSize">17</float>
<object class="IBUITextInputTraits" key="IBUITextInputTraits">
- <int key="IBUIReturnKeyType">9</int>
+ <int key="IBUIReturnKeyType">4</int>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
</object>
@@ -208,7 +208,7 @@
<bool key="IBUIAdjustsFontSizeToFit">YES</bool>
<float key="IBUIMinimumFontSize">17</float>
<object class="IBUITextInputTraits" key="IBUITextInputTraits">
- <int key="IBUIReturnKeyType">9</int>
+ <int key="IBUIReturnKeyType">4</int>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
</object>
@@ -283,6 +283,54 @@
</object>
<int key="connectionID">20</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">scrollView</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="206876602"/>
+ </object>
+ <int key="connectionID">21</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">txtIggle</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="696973480"/>
+ </object>
+ <int key="connectionID">22</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">txtNiggle</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="89122733"/>
+ </object>
+ <int key="connectionID">23</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">txtOggle</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="141163670"/>
+ </object>
+ <int key="connectionID">24</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">txtBogle</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="1024068394"/>
+ </object>
+ <int key="connectionID">25</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">txtSplat</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="325075817"/>
+ </object>
+ <int key="connectionID">26</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -404,7 +452,7 @@
<reference key="dict.values" ref="0"/>
</object>
<nil key="sourceID"/>
- <int key="maxID">20</int>
+ <int key="maxID">26</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -412,6 +460,66 @@
<object class="IBPartialClassDescription">
<string key="className">FirstViewController</string>
<string key="superclassName">UIViewController</string>
+ <object class="NSMutableDictionary" key="outlets">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <object class="NSArray" key="dict.sortedKeys">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <string>scrollView</string>
+ <string>txtBogle</string>
+ <string>txtIggle</string>
+ <string>txtNiggle</string>
+ <string>txtOggle</string>
+ <string>txtSplat</string>
+ </object>
+ <object class="NSMutableArray" key="dict.values">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <string>TPKeyboardAvoidingScrollView</string>
+ <string>UITextField</string>
+ <string>UITextField</string>
+ <string>UITextField</string>
+ <string>UITextField</string>
+ <string>UITextField</string>
+ </object>
+ </object>
+ <object class="NSMutableDictionary" key="toOneOutletInfosByName">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <object class="NSArray" key="dict.sortedKeys">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <string>scrollView</string>
+ <string>txtBogle</string>
+ <string>txtIggle</string>
+ <string>txtNiggle</string>
+ <string>txtOggle</string>
+ <string>txtSplat</string>
+ </object>
+ <object class="NSMutableArray" key="dict.values">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <object class="IBToOneOutletInfo">
+ <string key="name">scrollView</string>
+ <string key="candidateClassName">TPKeyboardAvoidingScrollView</string>
+ </object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">txtBogle</string>
+ <string key="candidateClassName">UITextField</string>
+ </object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">txtIggle</string>
+ <string key="candidateClassName">UITextField</string>
+ </object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">txtNiggle</string>
+ <string key="candidateClassName">UITextField</string>
+ </object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">txtOggle</string>
+ <string key="candidateClassName">UITextField</string>
+ </object>
+ <object class="IBToOneOutletInfo">
+ <string key="name">txtSplat</string>
+ <string key="candidateClassName">UITextField</string>
+ </object>
+ </object>
+ </object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/FirstViewController.h</string>
View
2  TPKeyboardAvoidingScrollView.h
@@ -7,6 +7,8 @@
@interface TPKeyboardAvoidingScrollView : UIScrollView {
UIEdgeInsets priorInset;
+ BOOL _keyboardVisible;
}
+- (void)adjustOffsetToIdealIfNeeded;
@end
View
15 TPKeyboardAvoidingScrollView.m
@@ -81,6 +81,7 @@ - (void)keyboardWillShow:(NSNotification*)notification {
[self setContentOffset:CGPointMake(self.contentOffset.x, offset) animated:YES];
[UIView commitAnimations];
+ _keyboardVisible = true;
}
- (void)keyboardWillHide:(NSNotification*)notification {
@@ -93,6 +94,7 @@ - (void)keyboardWillHide:(NSNotification*)notification {
self.contentInset = priorInset;
priorInset = UIEdgeInsetsZero;
[UIView commitAnimations];
+ _keyboardVisible = false;
}
- (UIView*)findFirstResponderBeneathView:(UIView*)view {
@@ -129,4 +131,17 @@ -(CGFloat)idealOffsetForView:(UIView *)view withSpace:(CGFloat)space
return offset;
}
+
+-(void)adjustOffsetToIdealIfNeeded
+{
+ //only do this if the keyboard is already visible
+ if (!_keyboardVisible) return;
+
+ CGFloat visibleSpace = self.bounds.size.height - self.contentInset.top - self.contentInset.bottom;
+
+ CGPoint idealOffset = CGPointMake(0, [self idealOffsetForView:[self findFirstResponderBeneathView:self] withSpace:visibleSpace]);
+
+ [self setContentOffset:idealOffset animated:YES];
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.