Ios 哪个视图应该注册键盘通知
我有一个导航控制器(a),其父视图控制器(不可见)将另一个导航控制器显示为模态表单(B) 我正试图弄清楚如何向上移动模态视图控制器,这样字段就不会被键盘遮挡,但我不确定要在哪个视图上侦听键盘通知,因此应该负责移动导航控制器(B) 另外一个问题是,我甚至不确定我是否可以移动模态表单视图控制器。我试着改变它的框架,但没有任何效果 (A)中表示(B)的代码在这里Ios 哪个视图应该注册键盘通知,ios,uinavigationcontroller,keyboard,Ios,Uinavigationcontroller,Keyboard,我有一个导航控制器(a),其父视图控制器(不可见)将另一个导航控制器显示为模态表单(B) 我正试图弄清楚如何向上移动模态视图控制器,这样字段就不会被键盘遮挡,但我不确定要在哪个视图上侦听键盘通知,因此应该负责移动导航控制器(B) 另外一个问题是,我甚至不确定我是否可以移动模态表单视图控制器。我试着改变它的框架,但没有任何效果 (A)中表示(B)的代码在这里 imageEditor.navigationItem.title = @"Photo Details Editor"; im
imageEditor.navigationItem.title = @"Photo Details Editor";
imageEditor.edgesForExtendedLayout = UIRectEdgeNone;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:imageEditor];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(imageEditorDone)];
navController.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(imageEditorCancelled)];
navController.preferredContentSize = imageEditor.view.frame.size;
[self.parentViewController presentViewController:navController animated:YES completion:^(void){ }];
我该如何继续
谢谢
这是我不久前使用的一个示例,您可以修改它以适用于您的案例。基本上,我计算键盘和视图的交点,然后设置视图y轴的动画以补偿键盘
#pragma mark - Keyboard Event Responses
-(void)observeKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToKeyboardAppearanceNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToKeyboardDisappearanceNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)respondToKeyboardAppearanceNotification:(NSNotification *)inNotification {
// Workaround to the invalid animation curve returned below (<< 16). Source: https://devforums.apple.com/message/878410#878410
NSUInteger tmpOption = [[inNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect tmpKeyboardBeginFrame = [[inNotification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect tmpKeyboardEndFrame = [[inNotification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
if(fabs(tmpKeyboardBeginFrame.origin.y- tmpKeyboardEndFrame.origin.y) < 50) {
// iOS 9 fires this notification when the first responder changes even if the keyboard is present
// This check will stop the appearance block from firing if the keyboard isn't actually appearing
return;
}
CGFloat heightAdjustment = [self intersectionOfView:self keyboardRect:tmpKeyboardEndFrame].size.height;
self.alertCenterY.constant = -heightAdjustment;
[UIView animateWithDuration:[[inNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0.0f
options:(tmpOption << 16)
animations:
^{
[self.containerView layoutIfNeeded];
}
completion:nil];
}
-(void)respondToKeyboardDisappearanceNotification:(NSNotification *)inNotification {
// Workaround to the invalid animation curve returned here (<< 16). Source: https://devforums.apple.com/message/878410#878410
NSUInteger tmpOption = [[inNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
self.alertCenterY.constant = 0;
[UIView animateWithDuration:[[inNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0.0f
options:(tmpOption << 16)
animations:
^{
[self.containerView layoutIfNeeded];
}
completion:nil];
}
- (CGRect)intersectionOfView:(UIView *)inView keyboardRect:(CGRect)inKeyboardRect {
CGRect tmpSelfFrame = CGRectZero;
CGRect tmpKeyboardFrame = inKeyboardRect;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
// Infer the keyboard's position in landscape. As it's attached to the window, its frame is fixed in portrait.
tmpKeyboardFrame = CGRectMake(0, CGRectGetWidth([self.window frame]) - CGRectGetWidth(tmpKeyboardFrame), CGRectGetHeight(tmpKeyboardFrame), CGRectGetWidth(tmpKeyboardFrame));
tmpSelfFrame = inView.frame;
} else {
tmpSelfFrame = [self.window convertRect:inView.frame toView:self.window];
}
return CGRectIntersection(tmpKeyboardFrame, tmpSelfFrame);
}
#pragma标记-键盘事件响应
-(无效)observeKeyboardNotifications{
[[NSNotificationCenter defaultCenter]移除观察者:self];
[[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(RespondtokeBoardAppearanceNotification:)名称:UIKeyboardWillShowNotification对象:nil];
[[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(RespondtokeBoardisAppearanceNotification:)名称:UIKeyboardWillHideNotification对象:nil];
}
-(无效)响应BoardAppearanceNotification:(NSNotification*)创新{
//解决下面返回的无效动画曲线()的问题,或者您可以尝试将imageEditor内容包装到中,然后休息一下
它将自动检测是否有任何UIControl元素成为第一响应者,并自动偏移以避免重叠。当键盘出现时,表单页应自动向上滚动。您无需执行任何操作即可实现此目的。@rmaddy我编辑代码以显示我的呈现方式(B),而且它没有移动,正如我的截图所示,所以我不知道我做错了什么。我有点困惑。表格只有在iPad处于横向时才会移动。哇,我得检查一下。