我在一个视图中有一个文本视图、一些装饰等,我希望该视图停靠在键盘上,就像任何其他消息传递应用程序一样。
当我要显示我的文本视图时,以下是我将视图附加到键盘的方式:
-(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 存储库来演示该问题: