视图设置为 inputAccessoryView 在添加回普通视图时抛出异常

View set as inputAccessoryView throws exception when added back into normal view

我在一个视图中有一个文本视图、一些装饰等,我希望该视图停靠在键盘上,就像任何其他消息传递应用程序一样。

当我要显示我的文本视图时,以下是我如何将我的视图附加到键盘:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    UIView *inputView = self.textInputView; //connected at IB outlet from storyboard, also contains the text view itself.
    constraintsOfTextInputView = [self constraintsRelatedToView:inputView]; //to be able to add them again
    [[UIApplication sharedApplication].keyWindow addSubview:self.textInputView];
    textView.inputAccessoryView = inputView;
    editingTextView = textView;
    return YES;
}

解雇时:

//using notification hook for UIKeyboardWillHideNotification because
//textView[Will|Did]EndEditing is called too late

-(void)keyboardWillHide{
    if(editingTextView && constraintsOfTextInputView){
        editingTextView.inputAccessoryView = nil;
        [self.textInputView removeFromSuperview];
        [self.view addSubview:self.textInputView]; <--- EXCEPTION
        [self.view addConstraints:constraintsOfTextInputView];
        [self.view layoutIfNeeded];

        editingTextView = nil;
        constraintsOfTextInputView = nil;
    }
}

即使我在添加时所做的与我所做的完全相反,我还是遇到了这个异常:

*** Terminating app due to uncaught exception
'UIViewControllerHierarchyInconsistency', reason: 'child view controller:
<UICompatibilityInputViewController: 0x13307ba20> should have parent view
controller:<ULPostViewController: 0x1307a7c00> but actual parent is:
<UIInputWindowController: 0x12f0be200>'

我怎样才能摆脱这个问题?我在 iOS 8(不支持旧版本)。

更新: 我创建了一个 git 存储库来演示问题:

https://github.com/can16358p/CPInputAccessoryTest

你在滥用 inputView 而不是。输入视图应该用于查看用户输入数据,输入附件视图是您可能想要扩展的东西。但它应该是类似于键盘的东西。

您的文本视图不是其中的一部分。这是用户输入数据 的视图。决定是否要将视图作为输入视图或在主视图层次结构中,但不要在两者之前移动它。

您真正的问题是您想要在键盘出现时移动文本视图以使其保持可见。为此,请观察键盘通知

UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification

并相应地调整您的观点。您可以调整视图控制器的视图的边界原点,例如,将所有内容向上移动。

您可以将文本视图用作整个视图控制器的输入附属视图。参见 my pull request

除了不断增长的文本视图部分,这几乎是消息应用程序的行为。