我使用以下代码在点击文本字段时返回键盘。
当我用UIKeyboardBoundsUserInfoKey
替换UIKeyboardFrameBeginUserInfoKey
时,因为旧版本显示警告它已在xcode4中弃用。
它是否适用于iOS 3
设备,因为它是iOS sdk 4
的新API?
-(void)keyboardWasShown:(NSNotification *)aNotification {
if ( keyboardShown )
return;
if ( (activeField==tfNet)||(activeField==tfSync)||(activeField==tfFqone)||(activeField==tfFqtwo)||(activeField==tfFqthree)||(activeField==tfFqfour)||(activeField==tfFqfive)||(activeField==tfLqone)||(activeField==tfLqtwo)||(activeField==tfLqthree)||(activeField==tfLqfour)||(activeField==tfLqfive))
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED <= __IPHONE_3_2
CGRect _keyboardEndFrame;
[[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGFloat keyboardSize;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
keyboardSize = _keyboardEndFrame.size.height;
}
else {
keyboardSize = _keyboardEndFrame.size.width;
}
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize-8;
frame.size.height += keyboardSize-8;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
#else
NSDictionary *info = [aNotification userInfo];
//NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize.height-8;
frame.size.height += keyboardSize.height-8;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
#endif
viewMoved = YES;
}
keyboardShown = YES;
}
- (void)keyboardWasHidden:(NSNotification *)aNotification {
if ( viewMoved ) {
#if __IPHONE_OS_VERSION_MIN_REQUIRED <= __IPHONE_3_2
CGRect _keyboardEndFrame;
[[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGFloat keyboardSize;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
keyboardSize = _keyboardEndFrame.size.height;
}
else {
keyboardSize = _keyboardEndFrame.size.width;
}
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize-8;
frame.size.height -= keyboardSize-8;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
#else
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-8;
frame.size.height -= keyboardSize.height-8;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
#endif
viewMoved = NO;
}
keyboardShown = NO;
}