简体   繁体   English

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

[英]Swift 3: InputAccessoryView instantiated from .xib file

I'm having a problem with instantiating my InputAccessoryView from a .xib file, where the inputAccessoryView doesn't react on input: 我在从.xib文件中实例化我的InputAccessoryView时遇到问题,其中inputAccessoryView对输入没有反应:

private var conversationToolBar = ConversationToolBar()

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

在此输入图像描述

If I instantiate my inputAccessoryView from a method (createAccessoryView): 如果我从方法(createAccessoryView)实例化我的inputAccessoryView:

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

My class: 我的课:

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
}
}

The behaviour works as expected. 行为按预期工作。

在此输入图像描述

Can anyone help me? 谁能帮我? Thank you 谢谢

I had same issue with showing inputAccessoryView as expected , and fixed it with calling this method to refresh input or accessory view : 我有同样的问题,显示inputAccessoryView按预期,并修复它调用此方法刷新输入或附件视图:

closeLabel.reloadInputViews()

Updates the custom input and accessory views when the object is the first responder. 当对象是第一响应者时更新自定义输入和附件视图。 You can use this method to refresh the custom input view or input accessory view associated with the current object when it is the first responder. 当第一响应者作为第一响应者时,您可以使用此方法刷新自定义输入视图或输入与当前对象关联的附件视图。 The views are replaced immediately—that is, without animating them into place. 视图会立即替换 - 也就是说,不会将它们设置为动画。 If the current object is not the first responder, this method has no effect. 如果当前对象不是第一个响应者,则此方法无效。

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

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