1

我在从 .xib 文件中实例化 InputAccessoryView 时遇到问题,其中 inputAccessoryView 对输入没有反应:

private var conversationToolBar = ConversationToolBar()

override var inputAccessoryView: UIView? {
    return self.conversationToolBar
}

在此处输入图像描述

如果我从方法 (createAccessoryView) 实例化我的 inputAccessoryView:

private var conversationToolBar = ConversationToolBar().createAccessoryView(width: UIScreen.main.bounds.width)

我的课:

class ConversationToolBar: UIView {

@IBOutlet var view: UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    self.view = UINib(nibName: "ConversationToolBar", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
    self.addSubview(self.view)
    self.view.frame = self.bounds
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.view = UINib(nibName: "ConversationToolBar", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
    self.addSubview(self.view)
    self.view.frame = self.bounds
}

func createAccessoryView(width: CGFloat) -> UIView {
    let accessoryView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 100))
    accessoryView.backgroundColor = UIColor.blue

    let closeLabel = UITextField(frame: accessoryView.frame)
    closeLabel.font = UIFont.boldSystemFont(ofSize: 14)
    closeLabel.text = "Hello"
    closeLabel.textColor = UIColor.white
    closeLabel.textAlignment = .center
    accessoryView.addSubview(closeLabel)

    return accessoryView
}
}

该行为按预期工作。

在此处输入图像描述

谁能帮我?谢谢

4

1 回答 1

0

我在按预期显示时遇到了同样的问题inputAccessoryView,并通过调用此方法来刷新输入或附件视图来修复它:

closeLabel.reloadInputViews()

当对象是第一响应者时更新自定义输入和附件视图。当当前对象为第一响应者时,您可以使用此方法刷新与当前对象关联的自定义输入视图或输入附件视图。视图会立即被替换——也就是说,不会将它们设置为动画。如果当前对象不是第一响应者,则此方法无效。

于 2017-05-11T07:04:40.917 回答