为什么键盘快速出现时顶视图也会移动

问题描述 投票:0回答:1

[我正在使用通知,在点击文本框时下视图向上移动,但是这里上视图也随着键盘移动向上移动,但是我不希望上面视图通过键盘移动,我只希望tableview通过键盘向上移动。

我在滚动视图中添加了表格视图

像上面这样的视图也可以通过键盘移动:

enter image description hereenter image description here

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
  @objc func keyboard(notification:Notification) {
        guard let keyboardReact = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else{
            return
        }

        if notification.name == UIResponder.keyboardWillShowNotification ||  notification.name == UIResponder.keyboardWillChangeFrameNotification {
            self.view.frame.origin.y = -keyboardReact.height
        }else{
            self.view.frame.origin.y = 0
        }
    }
ios swift uitableview view keyboard
1个回答
0
投票

此特定行是移动视图的行

self.view.frame.origin.y = -keyboardReact.height

您必须将其更改为

self.tableView.frame.origin.y = -keyboardReact.height

这就是行动

© www.soinside.com 2019 - 2024. All rights reserved.