简体   繁体   中英

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:

private var conversationToolBar = ConversationToolBar()

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

在此输入图像描述

If I instantiate my inputAccessoryView from a method (createAccessoryView):

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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