【发布时间】:2020-02-07 12:51:55
【问题描述】:
帮我解决这个问题。 我有一个 tableView,单元格包含 UILable 在点击单元格或标签时,我需要使用 inputAccessoryView(带有 textField)显示键盘。 编辑完成后,将 textField 中的文本传递给单元格中的 UILable,关闭键盘并完全隐藏 inputAccessoryView。
现在我用 inputAccessoryView 解决了这个问题,我使用 textField.becomeFirstResponder(),但在编辑 inputAccessoryView 后仍然可见。
!!!我需要 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 在编辑前后完全隐藏。不在屏幕上。现在在编辑之前和之后它位于屏幕底部。
-
您没有尝试创建视图并将其保存在某个全局变量中吗?并实时使用
inputAccessoryView = nil和inputAccessoryView = customView -
我当然试过了!但是...您可以隐藏 inputAccessoryView = nil,但不能再次添加。如何返回它,例如,通过按?
标签: ios swift keyboard uilabel inputaccessoryview