编辑 UILable,隐藏输入附件视图 [swift 5]



帮我解决这个问题。 我有一个表格视图,单元格包含 UILable 我需要在录制单元格或标签时显示带有inputAccessoryView(带有textField(的键盘。 编辑后,将文本从文本字段传递到单元格中的UILable,关闭键盘并完全隐藏inputAccessoryView。

现在我正在使用inputAccessoryView解决这个问题,我textField.bebecomeFirstResponder((,但是在编辑inputAccessoryView后仍然可见。

!!我需要在编辑前后完全隐藏输入附件视图。不在屏幕上。现在编辑之前和之后它位于屏幕底部

编辑前

编辑期间

编辑后

这是我的代码。

import UIKit
class TableViewController: UITableViewController {
override var canBecomeFirstResponder: Bool {
return true
}
var customInputView: UIView!
var sendButton: UIButton!
var addMediaButtom: UIButton!
let textField = FlexibleTextView()

override var inputAccessoryView: UIView? {
if customInputView == nil {
customInputView = CustomView()
customInputView.backgroundColor = UIColor.groupTableViewBackground
textField.placeholder = "I'm gonna grow in height."
textField.font = .systemFont(ofSize: 15)
textField.layer.cornerRadius = 5
customInputView.autoresizingMask = .flexibleHeight
customInputView.addSubview(textField)
sendButton = UIButton(type: .system)
sendButton.isEnabled = true
sendButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
sendButton.setTitle("Send", for: .normal)
sendButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
customInputView?.addSubview(sendButton)
addMediaButtom = UIButton(type: .custom)
addMediaButtom.setImage(UIImage(imageLiteralResourceName: "addImage").withRenderingMode(.alwaysTemplate), for: .normal)
addMediaButtom.isEnabled = true
//addMediaButtom.titleLabel?.font = UIFont.systemFont(ofSize: 16)
// addMediaButtom.setTitle("Media", for: .normal)
addMediaButtom.contentEdgeInsets = UIEdgeInsets(top: 9, left: 0, bottom: 5, right: 0)
addMediaButtom.addTarget(self, action: #selector(handleSend), for: .touchUpInside) 
customInputView?.addSubview(addMediaButtom)
textField.translatesAutoresizingMaskIntoConstraints = false
sendButton.translatesAutoresizingMaskIntoConstraints = false
addMediaButtom.translatesAutoresizingMaskIntoConstraints = false
sendButton.setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.horizontal)
sendButton.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.horizontal)
addMediaButtom.setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.horizontal)
addMediaButtom.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: NSLayoutConstraint.Axis.horizontal)
textField.maxHeight = 80
addMediaButtom.leadingAnchor.constraint(
equalTo: customInputView.leadingAnchor,
constant: 8
).isActive = true
addMediaButtom.trailingAnchor.constraint(
equalTo: textField.leadingAnchor,
constant: -8
).isActive = true
/*  addMediaButtom.topAnchor.constraint(
equalTo: customInputView.topAnchor,
constant: 8
).isActive = true
*/
addMediaButtom.bottomAnchor.constraint(
equalTo: customInputView.layoutMarginsGuide.bottomAnchor,
constant: -8
).isActive = true
textField.trailingAnchor.constraint(
equalTo: sendButton.leadingAnchor,
constant: 0
).isActive = true
textField.topAnchor.constraint(
equalTo: customInputView.topAnchor,
constant: 8
).isActive = true
textField.bottomAnchor.constraint(
equalTo: customInputView.layoutMarginsGuide.bottomAnchor,
constant: -8
).isActive = true
sendButton.leadingAnchor.constraint(
equalTo: textField.trailingAnchor,
constant: 0
).isActive = true
sendButton.trailingAnchor.constraint(
equalTo: customInputView.trailingAnchor,
constant: -8
).isActive = true
sendButton.bottomAnchor.constraint(
equalTo: customInputView.layoutMarginsGuide.bottomAnchor,
constant: -8
).isActive = true
}
return customInputView
}
@objc func handleSend() {
print("works")
self.textField.resignFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.keyboardDismissMode = .interactive
// Uncomment the following line to preserve selection between presentations
self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}


@IBAction func test(_ sender: Any) {
textField.becomeFirstResponder()
}
}
class CustomView: UIView {
// this is needed so that the inputAccesoryView is properly sized from the auto layout constraints
// actual value is not important
override var intrinsicContentSize: CGSize {
return CGSize.zero
}
}

您可以为每个文本字段添加inputAccessoryView。仅当您通过当前文本字段输入文本时,它才会出现。

要设置它,请使用:

textfield.inputAccessoryView = customView

如果你想完全删除它,请使用它(但这是不必要的(

textfield.inputAccessoryView = nil

以隐藏它。

附言最好通过 xib 视图创建困难的视图。

要在点击"发送"按钮时摆脱附件视图,您需要防止表视图再次成为第一响应者。

将此添加到您的类中:

var bEditing: Bool = false

然后更改canBecomeFirstResponder()handleSend()

override var canBecomeFirstResponder: Bool {
return !bEditing
}
@objc func handleSend() {
print("works")
bEditing = true
self.textField.resignFirstResponder()
bEditing = false
}

最新更新