哪个视图应该注册键盘通知

时间:2016-04-18 17:09:50

标签: ios uinavigationcontroller keyboard

我有一个导航控制器(A),其父视图控制器(不可见)将另一个导航控制器显示为模态表单(B)。

我正在试图弄清楚如何移动模态视图控制器,以便字段不被键盘遮挡,但我不确定哪个视图可以收听键盘通知,因此应负责移动导航控制器(B)。

作为一个额外的问题,我不确定我是否可以移动模态表格视图控制器。我尝试改变它的框架,但那没有做任何事情。

(A)中的代码(B)在这里......

    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){ }];

我如何前往这里?

感谢。

enter image description here

2 个答案:

答案 0 :(得分:0)

以下是我使用了一段时间的示例,您可以修改它以适用于您的情况。基本上我正在计算键盘和我的视图的交点,然后将视图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);
}

答案 1 :(得分:0)

或者您可以尝试将imageEditor内容包装到TPKeyboardAvoidingScrollView中并休息一下。

它会自动检测是否有任何UIControl元素成为第一响应者并自行偏移以避免重叠。