Created
December 12, 2012 23:33
-
-
Save casspangell/4272698 to your computer and use it in GitHub Desktop.
Dynamic Keyboards and Views ObjC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface SignUpViewController : UIViewController <UITextFieldDelegate> { | |
BOOL isLastTextField; | |
CGFloat adjustmentOffset; | |
CGFloat mainViewHeight; | |
CGFloat subViewHeight; | |
CGFloat keyboardY; | |
NSInteger textFieldY; | |
CGRect textFieldFrame; | |
} | |
@property (retain, nonatomic) IBOutlet UITextField *email; | |
@property (retain, nonatomic) IBOutlet UITextField *password; | |
@property (retain, nonatomic) IBOutlet UITextField *passwordConfirm; | |
@property (assign, nonatomic) IBOutlet UIView *textFieldView; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define KEYBOARD_HEIGHT 216 | |
@interface SignUpViewController () | |
@end | |
@implementation SignUpViewController | |
@synthesize email, password, passwordConfirm, textFieldView; | |
#pragma mark - Keyboard Delegates | |
-(void) keyboardDidShow: (NSNotification *)notif | |
{ | |
// Grab view height dynamically (needed for iPhone 4 and 5) | |
mainViewHeight = [[UIScreen mainScreen] bounds].size.height; | |
keyboardY = mainViewHeight - KEYBOARD_HEIGHT; | |
} | |
-(void) keyboardDidHide: (NSNotification *)notif | |
{ | |
if(isLastTextField){ | |
// slide view down when hidden | |
[UIView animateWithDuration:0.2 | |
animations:^{ | |
[[self textFieldView] setTransform:CGAffineTransformIdentity]; | |
}]; | |
} | |
// Reset offset | |
adjustmentOffset = 0; | |
} | |
- (void)textFieldDidBeginEditing:(UITextField *)textField | |
{ | |
// Use "next" until final field "done" | |
if (textField != ([textfieldsArr objectAtIndex:[textfieldsArr count]-1])) { | |
textField.returnKeyType = UIReturnKeyNext; | |
} | |
textFieldFrame = textField.frame; | |
CGPoint textFieldLocation = textFieldFrame.origin; | |
textFieldY = textFieldLocation.y; | |
// The keyboard hit the middle of the textfield so I adjusted by 35px | |
if(fabsf(keyboardY - textFieldY) < 35){ | |
adjustmentOffset -= fabsf(keyboardY - textFieldY); | |
} | |
// slide view up when keyboard shown | |
[UIView animateWithDuration:0.4 | |
animations:^{ | |
[[self textFieldView] setTransform:CGAffineTransformMakeTranslation(0, adjustmentOffset)]; | |
}]; | |
} | |
- (void)textFieldDidEndEditing:(UITextField *)textField | |
{ | |
// reset scrollview once keyboard hides on last field | |
if(textField == [textfieldsArr objectAtIndex:[textfieldsArr count]-1]){ | |
isLastTextField = YES; | |
}else{ | |
isLastTextField = NO; | |
} | |
} | |
-(BOOL)textFieldShouldReturn:(UITextField*)textField; | |
{ | |
for(int i=0; i<[textfieldsArr count]-1; i++){ | |
if(textField == [textfieldsArr objectAtIndex:i]){ | |
// resign keyboard to current textfield | |
[textField resignFirstResponder]; | |
// make the next textfield firstResponder | |
if(i <= [textfieldsArr count]){ | |
// else just become firstresponder | |
[[textfieldsArr objectAtIndex:i+1] becomeFirstResponder]; | |
} | |
} | |
// reaches "done" | |
[textField resignFirstResponder]; | |
} | |
return NO; // We do not want UITextField to insert line-breaks. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment