【问题标题】:Constraint UITableView to InputAccessory将 UITableView 约束到 InputAccessory
【发布时间】:2018-12-29 08:56:35
【问题描述】:

我的应用中有一个类似聊天的 UI,所以我有一个底栏,即 inputAccessoryView 和我的 tableView。我想要做的是在键盘在屏幕上和屏幕外时正确调整 tableView 的大小。但是,简单地将 tableView 的底部限制到 inputAccessoryView 的顶部似乎是不可能的。那么如何在我的应用中实现这种类似消息的行为呢?

viewDidLoad() 用于我的控制器

override func viewDidLoad() {

    super.viewDidLoad()
    noteTableView.transform = CGAffineTransform(rotationAngle: -(CGFloat)(Double.pi));
    noteTableView.keyboardDismissMode = .interactive


    noteTableView.delegate = self
    noteTableView.dataSource = self

    [...]

}

这是让我的 inputAccessoryView 实际工作的两个函数

override var inputAccessoryView: UIView? {
        get {
            return inputBar
        }
}

override var canBecomeFirstResponder: Bool {
        return true
}

我在 inputAccessory 内的 textField 上也有一个点击手势识别器,它将其行为从“按钮”切换到正确的文本字段,所以这里有一点。

@objc func newButtonTapped(tapGestureRecognizer: UITapGestureRecognizer) {

    [...]

    inputBar.inputField.isSelectable = true
    inputBar.inputField.isEditable = true
    inputBar.inputField.becomeFirstResponder()

    [...]

}

【问题讨论】:

    标签: swift keyboard tableview dismiss inputaccessoryview


    【解决方案1】:

    iOS 15中,Apple 引入了keyboardLayoutGuide,它是一个UILayoutGuide,可随着键盘的变化而动态变化(甚至是 iPad 上的键盘浮动模式)。

    所以你要做的就是:

    myTableView.bottomAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.TopAnchor)
    

    Apple's Developer Documentation 上阅读有关键盘布局指南的更多信息或观看WWDC21 Session

    【讨论】:

      猜你喜欢
      • 2020-09-07
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多