简体   繁体   English

自定义 inputaccessoryView 中的 UITextView 未退出第一响应者状态

[英]UITextView in custom inputaccessoryView not resigning first responder status

I have a chat application that implements a floating text input field (similar to the iOS Messages app) as the inputAccessoryView of my ChatViewController (see Apple's documentation ).我有一个聊天应用实现了一个浮动文本输入框(类似于iOS的消息应用程序)作为inputAccessoryView我的ChatViewController (见苹果的文档)。

class ChatViewController: UIViewController {

    override var inputAccessoryView: UIView? {
       return chatInputView
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }
    ...

My ChatViewController has a ChatTableViewController child view controller, which has cells containing UITextField s whose text content is editable.我的ChatViewController有一个ChatTableViewController子视图控制器,它有包含UITextField的单元格,其文本内容是可编辑的。 The issue I'm running into is that when the user taps on a cell's UITextField , the inputAccessoryView 's UITextView refuses to resign first responder status, which prevents the content in the UITableViewCell from being edited.我遇到的问题是,当用户点击单元格的UITextFieldinputAccessoryViewUITextView拒绝inputAccessoryView第一响应者状态,这会阻止UITableViewCell的内容被编辑。 The following warning is logged in the console:控制台中记录了以下警告:

 First responder warning: '<UITextView: 0x7fc041041c00; 
 frame = ...' rejected resignFirstResponder when being removed from hierarchy

I've tried calling resignFirstResponder and endEditing on the UIInputView and UITextView directly with no success.我试过直接在UIInputViewUITextView上调用resignFirstResponderendEditing ,但没有成功。 I don't want the ChatViewController to resign first responder status as that would cause the inputAccessoryView to disappear.我不希望ChatViewController退出第一响应者状态,因为这会导致inputAccessoryView消失。

Edit: I also receive the warning when dismissing the keyboard interactively (instead of tapping on the UITableViewCell 's UITextField .编辑:我在以交互方式关闭键盘时也会收到警告(而不是点击UITableViewCellUITextField

Could you please check the following:能否请您检查以下内容:

If UITextView is removed from super view:如果UITextView从超级视图中移除:

  • Is the UITextView being removed from the view hierarchy ? UITextView是否从视图层次结构中删除?
  • If so could you resignFirstResponder before removing it from the super view.如果是这样,您resignFirstResponder在将其从超级视图中删除之前先resignFirstResponder

If UITextView subclass is being used:如果正在使用UITextView子类:

  • Are you using a subclass of UITextView , if so have you implemented canResignFirstResponder to return false .您是否使用UITextView的子类,如果是,您是否实现了canResignFirstResponder以返回false
  • Could you try returning true instead.你可以尝试返回true吗?

My solution:我的解决方案:

  1. override canBecomeFirstResponder to your inputAccessoryView and that UITextField on inputAccessoryView覆盖 canBecomeFirstResponder 到您的 inputAccessoryView 和 inputAccessoryView 上的 UITextField
  2. when user pressed hide keyboard or return, set canBecomeFirstResponder to false当用户按下隐藏键盘或返回时,将 canBecomeFirstResponder 设置为 false
  3. remove inputAccessoryView from its superView从其 superView 中删除 inputAccessoryView

still have warning message when press hide keyboard, but works well for me.按隐藏键盘时仍然有警告消息,但对我来说效果很好。

This warning is also shown when using the iOS message app and whatsApp.使用 iOS 消息应用程序和 WhatsApp 时也会显示此警告。 There is no known way to get rid of that.没有已知的方法可以摆脱它。 It has no negative impact, I believe we should just ignore it.它没有负面影响,我认为我们应该忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM