As promised i wanna help you all because i found some troubles solving this issue.
In fact for this, Apple reviewers rejected my console game "Hack Online".
Let's start!
Now we need a BOOL to track keyboard open or not. Usually i use it in the template.h with the extern method, but you can do how you want:
template.h
Add this line:
extern bool keyb_open;
template.cpp
Add this line:
bool keyb_open = false;
UntitledViewController.h
Add those lines:
- (void) keyboardWillShow:(NSNotification*)notification;
- (void) keyboardWillHide;
UntitledViewController.m
Now in the .m file we have some things to do...
The first is this, add those lines (don't mind if i call different functions, if you wanna rename it to don't get confused you can):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}
Now we need to add the observer functions...
- (void) keyboardDidShow:(NSNotification*)notification
{
NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat deviceWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat newKeyboardHeight;
UIInterfaceOrientation interfaceOrientation = appDelegate.viewController.interfaceOrientation;
if(interfaceOrientation == UIInterfaceOrientationPortrait){
newKeyboardHeight = deviceHeight - keyboardRect.origin.y;
} else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
newKeyboardHeight = keyboardRect.size.height + keyboardRect.origin.y;
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
newKeyboardHeight = deviceWidth - keyboardRect.origin.x;
} else {
newKeyboardHeight = keyboardRect.size.width + keyboardRect.origin.x;
}
if(newKeyboardHeight > 0){
keyb_open = true;
[self animateTextField:command_line up:YES];
} else {
if(keyb_open){
keyb_open = false;
[self animateTextField:command_line up:NO];
} else {
keyb_open = true;
}
}
NSLog(@"Height : %f", newKeyboardHeight);
}
- (void) keyboardWillHide
{
CGRect frame = [[self.view.layer presentationLayer] frame];
NSLog(@"X : %f - Y : %f", frame.origin.x, frame.origin.y);
if(frame.origin.x < 0 && keyb_open){
[self animateTextField:command_line up:NO];
}
keyb_open = false;
}
- (void) animateTextField: (UITextField*)textField up:(BOOL)up
{
//This is my personal way to discover the device (real_width) you can use what you want
int txtPosition = 0;
if(textField.tag == 1){
if(Opzioni[0].real_width == 568){
txtPosition = (textField.frame.origin.y - 135);
} else if(Opzioni[0].real_width == 480){
txtPosition = (textField.frame.origin.y - 135);
} else if(Opzioni[0].real_width > 568){
txtPosition = (textField.frame.origin.y - 380);
}
}
const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, movement, 0);
[UIView commitAnimations];
}
Important!!!!
This is the part that do the trick...
We need to add a inputAccessoryView to the UITextField we use like this:
CGRect frame = CGRectMake(x, y, w, h);
textfield = [[UITextField alloc] initWithFrame:frame];
textfield.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];
This will create an invisible keyboard that will be recognized with a size of 0 in the keyboardDidShow.
In this way he found 0 and know it is an external keyboard.
The trick is when the extrenal keyboard close the virtual keyboard, it return to the keyboardDidShow with NOSENSE!!!!! (I think an Apple bug)
Well in this way you can handle it.
Is not very beautiful to see the white part when closing, but better than nothing!!!
And finally ENJOY!!!
Long life to Steve!