ios - 键盘打开时移动视图的问题
问题描述
当键盘打开时,我在移动文本输入和按钮时遇到问题。我正在使用以下代码
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillChange(notification: NSNotification) {
guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
view.frame.origin.y = -keyboardSize.height
} else {
view.frame.origin.y = 0
}
}
当我开始在文本字段中输入时出现问题,这里是之前的图像:https ://imgur.com/a/cbQbJzW和之后:https ://imgur.com/a/f1Nakrs
我很抱歉文件的语言没有说任何空间。
我想知道为什么会发生这种情况,是否可能是因为我在灰色文本字段中使用 CocoaPods - YoshikoTextField ?
谢谢!
解决方案
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var backgroundScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
// Do any additional setup after loading the view, typically from a nib.
}
@objc func keyboardWillShow(notification:NSNotification) {
adjustingHeight(true, notification: notification)
}
@objc func keyboardWillHide(notification:NSNotification) {
adjustingHeight(false, notification: notification)
}
func adjustingHeight(_ isShow:Bool, notification:NSNotification) {
let userInfo = notification.userInfo!
let keyboardFrame = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let changeInHeight = (keyboardFrame.height + 20) * (isShow ? 1 : -1)
backgroundScrollView.contentInset.bottom += changeInHeight
backgroundScrollView.scrollIndicatorInsets.bottom += changeInHeight
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
推荐阅读
- matlab - 符号分化
- debugging - 使用 Mocha 和 Atom 进行交互式调试
- python - 熊猫在午夜后一天添加
- rust - 在递归函数中使用 impl Trait
- excel - 如何替换记录中的 GUID,然后在 Excel“唯一”公式中只返回唯一的?
- django - Django Signal没有将用户添加到组
- ios - iOS 应用程序通过分页使用多个 JSON
- c++ - 错误:无法将“int&”类型的非常量左值引用绑定到“int”类型的右值
- json - 我无法访问 json 响应 Swift 4 中的值
- node.js - 如何将数据从客户端的 html 发送到 node.js 服务器?