当用户单击UIWebView中的文本字段时出现键盘后屏幕不再移动
我有一个iPhone应用程序(Swift 3),其内容动态地从CMS加载登录。该表格包含用户需要输入和提交的字段,没有什么棘手的。当用户单击UIWebView中的文本字段时出现键盘后屏幕不再移动
不幸的是,当用户点击登录字段时,键盘出现,并且它隐藏了包含表单的UIWebView。
我该如何解决这个问题?
Please Check Screenshot, click the form hiding my UIWebView Contents
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y = -keyboardSize.height/2
}
}
func keyboardWillHide(notification: NSNotification) {
if let _ = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y = 0
}
}
试试这个代码,这会为工作斯威夫特3。在这种情况下,我给出了4个textFields
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField4: UITextField!
@IBOutlet weak var textField3: UITextField!
@IBOutlet weak var textField2: UITextField!
@IBOutlet weak var textField1: UITextField!
override func viewWillDisappear(_ animated: Bool)
{
view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.textField1.delegate = self
self.textField2.delegate = self
self.textField3.delegate = self
self.textField4.delegate = self
self.textField1.tag = 0
self.textField2.tag = 1
self.textField3.tag = 2
self.textField4.tag = 3
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField.tag {
case 0:
moveTextField(textField: textField, moveDistance: -120, up: true)
case 1:
moveTextField(textField: textField, moveDistance: -140, up: true)
case 2:
moveTextField(textField: textField, moveDistance: -160, up: true)
case 3:
moveTextField(textField: textField, moveDistance: -180, up: true)
default:
print("Do Nothing...!")
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
switch textField.tag {
case 0:
moveTextField(textField: textField, moveDistance: -120, up: false)
case 1:
moveTextField(textField: textField, moveDistance: -140, up: false)
case 2:
moveTextField(textField: textField, moveDistance: -160, up: false)
case 3:
moveTextField(textField: textField, moveDistance: -180, up: false)
default:
print("Do Nothing...!")
}
}
func moveTextField(textField: UITextField, moveDistance: Int, up: Bool)
{
let moveDuration = 0.3
let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveDuration)
self.view.frame = (self.view.frame).offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
}
对不起,@Grecory威尔逊Pullyattu你不明白我的问题,我在我的Webview中使用UIWebView和UITextField .....所以我如何实现你的代码? –
你可以添加你的textField任何地方,并实现这个代码。它会工作。但是此代码仅适用于4个文本字段。你可以添加更多的文本字段也可以通过添加更多的案例 –
只是尝试它。我希望它会工作 –
没有解决方案吗?即时通讯实现你的代码,但仍然显示相同的问题 –
你可以告诉我你的代码? – Rahuld
你确定等待会告诉你...但我在哪里告诉你我的代码? –