Skip to content

Instantly share code, notes, and snippets.

@casspangell
Created April 18, 2012 21:57
Show Gist options
  • Save casspangell/2416886 to your computer and use it in GitHub Desktop.
Save casspangell/2416886 to your computer and use it in GitHub Desktop.
Some Xcode Keyboard Delegates
#pragma mark - keyboard delegates
-(void) textFieldDidBeginEditing:(UITextField *)textFieldView
{
currentTextField = textFieldView;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == titleOfImageTextField)
{
[urlOfImageTextFiled becomeFirstResponder];
sendButton.hidden = NO;
}
else
{
assert(textField == urlOfImageTextFiled);
[textField resignFirstResponder];
if([titleOfImageTextField.text compare:@""] != NSOrderedSame)
{
sendButton.hidden = NO;
}
}
return YES;
}
-(void) textFieldDidEndEditing:(UITextField *) textFieldView
{
currentTextField = nil;
}
-(void) keyboardDidShow:(NSNotification *) notification
{
if (keyboardIsShown) {
return;
}
NSDictionary* info = [notification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
//Create offset for the view to move above the keyboard
offset = theScroll.contentOffset;
CGRect viewFrame = theScroll.frame;
viewFrame.size.height -= keyboardSize.height;
theScroll.frame = viewFrame;
CGRect textFieldRect = [currentTextField frame];
textFieldRect.origin.y += 100; //this number you might want to adjust
[theScroll scrollRectToVisible: textFieldRect animated:YES];
keyboardIsShown = YES;
}
-(void) keyboardDidHide:(NSNotification *) notification
{
if (!keyboardIsShown) {
return;
}
theScroll.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
theScroll.contentOffset = offset;
keyboardIsShown = NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment