har*_*alb 2 iphone notifications
我正在使用keyboardWasShown和keyboardWillBeHidden通知滑动视图以获得可见的文本视图.
我有一个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];
Run Code Online (Sandbox Code Playgroud)
和方法
- (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;
}
Run Code Online (Sandbox Code Playgroud)
你应该在eachClass中像这样:
-(void) viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[nc addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void) viewWillDisappear: (BOOL)animated{
[super viewWillDisappear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[nc removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
Run Code Online (Sandbox Code Playgroud)
因为通知是在应用程序级别而不是您的类级别.因此,如果您已将它们添加到一个类而不是所有类中,那么请转到下一个类.通知仍将keyboardWasShown:从您添加通知的类调用密钥和另一个,因此您的本地变量如... viewMoved = YES;
keyboardShown = YES;
Run Code Online (Sandbox Code Playgroud)
会抛出坏的超额例外
在您的情况下,还需要在所有6个视图控制器中执行此操作
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
7616 次 |
| 最近记录: |