gpt4 book ai didi

ios - 隐藏键盘后如何重新定位文本字段?

转载 作者:行者123 更新时间:2023-11-28 06:26:15 24 4
gpt4 key购买 nike

我在打字时向上推文本字段,因为键盘可能会覆盖文本字段。一切正常,文本字段被插入,但是当我隐藏键盘时,文本字段保持在其位置并且不会移动到它的正常位置。

import UIKit

// to remove keyboared when tapping around it
extension UIViewController {
func hideKeyboaredWhenTappedAround(){
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)

}
func dismissKeyboard(){
view.endEditing(true)
}
}
class SignInViewController: UIViewController, UITextFieldDelegate {
//MARK: Proporties
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contactUsButton: UIButton!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var forgotButton: UIButton!
@IBOutlet weak var registerButton: UIButton!
var activeField : UITextField? // #1
@IBOutlet weak var callButton: UIButton!

//MARK: View Cycle
override func viewDidLoad() {
super.viewDidLoad()

self.hideKeyboaredWhenTappedAround()
usernameTextField.delegate = self
passwordTextField.delegate = self

// #1
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: Pressed buttons
@IBAction func contactUsButtonPressed(_ sender: Any) {
}

@IBAction func forgotButtonPressed(_ sender: Any) {
}
@IBAction func registerButtonPressed(_ sender: Any) {
let registerVC = storyboard?.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController
self.navigationController?.pushViewController(registerVC, animated: true)

}

@IBAction func callButtonPressed(_ sender: Any) {
}

//MARK: To scroll keyboard
// to scroll the view up to use the keyboard #1
func keyboardWasShown(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.isScrollEnabled = true
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets

var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = self.activeField {
if (!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}
// #1
func keyboardWillBeHidden(notification: NSNotification){
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.isScrollEnabled = false
}

//MARK: TextField Delegate
func textFieldDidBeginEditing(_ textField: UITextField){
activeField = textField
}

func textFieldDidEndEditing(_ textField: UITextField){
activeField = nil
}

// func to hide keyboard when return is tapped
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}

最佳答案

你试过了吗IQKeyboardManager

关于ios - 隐藏键盘后如何重新定位文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com