【问题标题】:Swift - WhatsApp like keyboard dismissSwift - WhatsApp 就像键盘关闭
【发布时间】:2020-11-10 11:16:07
【问题描述】:

我正在开发一个消息应用程序,但我想在用手指按下时关闭键盘。

我知道可以这样做

keyboardDismissMode = .interactive

但我不知道如何在移动键盘时更改 tableview 的约束(例如 iOS 的 WhatsApp 应用)。

谁能帮帮我?

编辑:

这是我已经拥有的:

我有一个简单的 UITableView 来显示我的消息

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return messages.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    var celda = UITableViewCell()
    
    let message = messages[indexPath.row]
    
    if message.owner == 0{
        
        celda =  tableView.dequeueReusableCell(withIdentifier: "messageOther")!
    }else{
        
        celda =  tableView.dequeueReusableCell(withIdentifier: "messageMine")!
    }
    
    let fondoView = celda.viewWithTag(1)
    fondoView?.layer.cornerRadius = 10
               
    let messageLabel = celda.viewWithTag(2) as! UILabel
    messageLabel.text = message.text
    
    let fecha = Date(timeIntervalSince1970: TimeInterval(message.date))
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .short
    dateFormatter.timeZone = .current
    
    let dateLabel = celda.viewWithTag(3) as! UILabel
    dateLabel.text = dateFormatter.string(from: fecha)
    
    return celda
}

在我看来DidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    messagesTableView.keyboardDismissMode = .interactive
    
    getMessages()
}

键盘隐藏了用手指推动它,这就是我到目前为止所拥有的,但我不知道如何让我的 UITextField 跟随键盘的移动,就像 WhatsApp 和 iMessage 一样,也开始从UITextField 代替键盘。

感谢所有帮助!

【问题讨论】:

  • 你尝试了什么?你能分享你的代码吗?
  • 当然!我添加了更多信息
  • @Jorge 我遇到了同样的问题。你找到解决办法了吗?键盘将向下拖动,但 vstack 中的文本输入不会随之移动
  • 兄弟你找到任何解决方案

标签: swift xcode keyboard whatsapp dismiss


【解决方案1】:

您可以在ViewDidLoad 中将键盘关闭模式设置为onDrag,如下所示。

tableView.keyboardDismissMode = .onDrag

检查这是否是您想要的行为。

【讨论】:

  • 谢谢!我想要但用交互式而不是跟随手指的运动
猜你喜欢
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多