Swift 3:从.xib文件实例化的InputAccessoryView

时间:2017-05-10 14:43:15

标签: ios uiview swift3 uikeyboard inputaccessoryview

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

private var conversationToolBar = ConversationToolBar()

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

enter image description here

如果我从方法(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
}
}

行为按预期工作。

enter image description here

任何人都可以帮助我吗?谢谢

1 个答案:

答案 0 :(得分:0)

我出现了与预期inputAccessoryView相同的问题,并通过调用此方法来修复输入或附件视图来修复它:

closeLabel.reloadInputViews()
  

当对象是时,更新自定义输入和附件视图   急救人员。您可以使用此方法刷新自定义输入   查看或输入与当前对象关联的附件视图   它是第一个响应者。视图立即被替换 - 也就是说,   没有动画到位。如果当前对象不是   第一响应者,这种方法没有效果。