【发布时间】:2026-02-11 23:40:01
【问题描述】:
我在一个视图中有一个文本视图、一些装饰等,我希望该视图停靠在键盘上,就像任何其他消息传递应用程序一样。
当我要显示我的文本视图时,下面是我将视图附加到键盘的方法:
-(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 存储库来演示问题:
【问题讨论】:
-
inputAccessoryView无法添加到任何其他视图。除了将其设置为inputAccessoryView之外,为什么还要将其作为子视图添加到窗口中? -
@rmaddy 因为如果我不这样做,它就不会显示。
-
@CanPoyrazoğlu UIKit 应该负责显示您的输入视图。
-
@ChristianSchnorr 但事实并非如此。如果我不将它添加到窗口中,它就会在编辑开始时消失。
-
@CanPoyrazoğlu 你能在某处上传一个示例项目吗?这是不可能的。
标签: ios ios8 uikeyboard inputaccessoryview