I have a text view, some decorations etc. in a view and I want that view to dock on keyboard, just like any other messaging app.
When I'm about to display my text view, here is how I attach my view to the keyboard:
-(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;
}
And when dismissing:
//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;
}
}
Even though I'm doing exactly the opposite of what I do when adding, I'm getting this exception:
*** 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>'
How can I get rid of this problem? I'm on iOS 8 (and don't support older versions).
UPDATE: I've created a git repository demonstrating the problem:
https://github.com/can16358p/CPInputAccessoryTest
Copyright Notice:Content Author:「Can Poyrazoğlu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30766764/view-set-as-inputaccessoryview-throws-exception-when-added-back-into-normal-view