View set as inputAccessoryView throws exception when added back into normal view
NickName:Can Poyrazoğlu Ask DateTime:2015-06-11T04:33:42

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

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

Answers
Christian Schnorr 2015-06-11T10:46:21

You are abusing the inputView for something that it's not. The input view should be used for views the user an input data with, the input accessory view something you may want to extend that with. But its supposed be something like a keyboard.\n\nYour text view is NOT part of that. It is the view the user is inputting data into. Decide if you want the view as an input view or in the main view hierarchy, but don't move it around before the two.\n\nYour real problem is that you want to move the text view when the keyboard appears in order to keep it visible. To do so, observe the keyboard notifications\n\nUIKeyboardWillShowNotification\nUIKeyboardDidShowNotification\nUIKeyboardWillHideNotification\nUIKeyboardDidHideNotification\n\n\nand move your views around accordingly. You can adjust the view controller's view's bounds origin for example, to move everything up.",


More about “View set as inputAccessoryView throws exception when added back into normal view” related questions

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

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 vie...

Show Detail

Modal View destructor not being called when InputAccessoryView is overriden

I am presenting a UIViewController modally and that view contains various UITextFields. I've overriden the modal views InputAccessoryView so that it shows a simple view, with one button that will

Show Detail

InputAccessoryView with custom view created in IB

I've read a lot of material on this topic but most of them create custom view programatically. Is it possible to use InputAccessoryView with a custom view created in IB? In storyboard I've added

Show Detail

iPhone: How to fix inputAccessoryView to View?

I have a toolbar which I need to use when editing text, and when not. In previous apps, I've moved the toolbar manually (listening for notifications, etc.) But I want to use inputAccessoryView......

Show Detail

How can I show the inputAccessoryView in this view controller

I have a function in my controller I call private func toggleLauncher() { let launcher = CommentsLauncher() launcher.showLauncher() } This essentially adds a view on top of the current v...

Show Detail

inputAccessoryView not hide the view when -resignFirstResponder?

I have attached a toolbar with a UITextField and UIButton to the keyboard when it becomes the first responder via the user taping inside the textfield textField.inputAccessoryView = theToolbar;

Show Detail

How to prevent inputAccessoryView of a view controller from being shown when an alert is presented?

I have a custom UIViewController in which I have overridden inputAccessoryView to return a view as follows- extension LoginViewController { override var inputAccessoryView: UIView? {

Show Detail

Wrong View Height when using inputAccessoryView with External (blue tuth) keyboard

In my application i have set inputAccessoryView to the textfield. While i use Bluetooth keyboard it's giving me wrong height of view, Its subtracting device's keyboard height from the total iPad h...

Show Detail

How to remove inputAccessoryView and replace it with another view?

Here is the requirement Basic chat app that has an input accessory view on top of keyboard in a UICollectionViewController I add this custom input accessory view by using override var inputAccesso...

Show Detail

Xcode8 beta adding self.view.layoutIfNeeded() in inputAccessoryView getter causing crash

Using Xcode8 Beta my simulator crashes when using an inputAccessoryView and adding the line self.view.layoutIfNeeded() in the getter of the inputAccessoryView override. It works fine on my devices...

Show Detail