编辑 UILabel,隐藏 inputAccessoryView [swift 5]
Posted
技术标签:
【中文标题】编辑 UILabel,隐藏 inputAccessoryView [swift 5]【英文标题】:Edit UILable, hide inputAccessoryView [swift 5] 【发布时间】:2020-02-07 12:51:55 【问题描述】:帮我解决这个问题。 我有一个 tableView,单元格包含 UILable 在点击单元格或标签时,我需要使用 inputAccessoryView(带有 textField)显示键盘。 编辑完成后,将 textField 中的文本传递给单元格中的 UILable,关闭键盘并完全隐藏 inputAccessoryView。
现在我用 inputAccessoryView 解决了这个问题,我使用 textField.becomeFirstResponder(),但在编辑 inputAccessoryView 后仍然可见。
!!!我需要 inputAccessoryView 在编辑前后完全隐藏。不在屏幕上。现在在编辑之前和之后它位于屏幕底部
prior to editing
during editing
once edited
这是我的代码。
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 在编辑前后完全隐藏。不在屏幕上。现在在编辑之前和之后它位于屏幕底部。 您没有尝试创建视图并将其保存在某个全局变量中吗?并实时使用inputAccessoryView = nil
和inputAccessoryView = customView
我当然试过了!但是...您可以隐藏 inputAccessoryView = nil,但不能再次添加。如何返回它,例如,通过按?
【参考方案1】:
您可以为每个文本字段添加inputAccessoryView
。只有当您通过当前文本字段输入文本时才会出现。
设置它使用:
textfield.inputAccessoryView = customView
如果您想完全删除它,请使用它(但没有必要)
textfield.inputAccessoryView = nil
隐藏它。
附:最好通过xib-view创建困难视图。
【讨论】:
我隐藏了 inputAccessoryView = nil,但我无法再次添加它。点击单元格时如何取回它?【参考方案2】:要在点击“发送”按钮时摆脱附件视图,您需要防止 tableView 再次成为第一响应者。
将此添加到您的课程中:
var bEditing: Bool = false
然后更改canBecomeFirstResponder()
和handleSend()
:
override var canBecomeFirstResponder: Bool
return !bEditing
@objc func handleSend()
print("works")
bEditing = true
self.textField.resignFirstResponder()
bEditing = false
【讨论】:
以上是关于编辑 UILabel,隐藏 inputAccessoryView [swift 5]的主要内容,如果未能解决你的问题,请参考以下文章
在 UITableViewCell 中添加渐变隐藏 UILabel 文本