2
我正在使用keyboardWasShown
和keyboardWillBeHidden
通知來滑動視圖以獲取可見的文本視圖。UIKeyBoardWIllShowNotification調用一次的原因是什麼?
我有一個UITabBar
應用程序與六個選項卡。
在每個視圖中,我使用UINavigationController
。
在每個UITableViewCell
的詳細視圖中,我正在使用鍵盤通知。
所以問題是,我第一次使用鍵盤通知。在其他選項卡上它不會工作。
的代碼如下:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification
object:nil];
和方法
- (void)keyboardWasShown:(NSNotification *)aNotification {
if (keyboardShown)
return;
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize.height-100;
frame.size.height += keyboardSize.height-100;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = YES;
keyboardShown = YES;
}
- (void)keyboardWasHidden:(NSNotification *)aNotification {
if (viewMoved && tvAddreview) {
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-100;
frame.size.height -= keyboardSize.height-100;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = NO;
}
keyboardShown = NO;
}
您也可以從通知的userInfo中獲取animationDuration值。 – Morion 2009-12-11 13:22:59
這些方法和觀察者添加位置在哪裏? – Morion 2009-12-11 13:24:33
我在viewDidLoad – harshalb 2009-12-12 06:29:02