gpt4 book ai didi

ios - 如何计算和使用 "textFieldDidBeginEditing"中的键盘高度(快速),以便我可以精确地将 UITextfield 向上移动?

转载 作者:行者123 更新时间:2023-11-28 07:58:07 36 4
gpt4 key购买 nike

您可以将此视为我之前帖子的后续问题。

我已经编写了一个逻辑,只要它隐藏在键盘后面,我就可以向上移动文本字段。截至目前,我正在考虑键盘将覆盖所有设备底部 40% 的屏幕,因此根据设备大小移动文本字段。这当然是一个错误的假设,我需要先尝试计算键盘高度,然后再应用逻辑。

我对堆栈溢出的研究表明我可以通过 Notification 计算键盘高度。

虽然我按照堆栈溢出的指令编写了键盘高度的逻辑,但我不知道如何将我的通知代码和 textFieldDidBeginEditing 合并在一起以使其运行。

有没有人可以帮我解决这个问题?总之,我想在 textFieldDidBeginEditing 函数中使用键盘高度来使其动态化。

下面是我到目前为止写的一段代码:

键盘高度:

   func keyboardHeightNotification(){
NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil
)
}


@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
print(keyboardHeight)
}
}

向上移动文本字段代码(它工作正常但我需要合并键盘高度以使其动态化):

  func textFieldDidBeginEditing(_ textField: UITextField) {

if textField.frame.maxY > self.view.frame.height * 0.6
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - self.view.frame.height * 0.6 + 2.0), animated: true)

}

else{
return
}

print(textField.frame.maxY)
print(self.view.frame.height * 0.6)
print(textField.frame.maxY - self.view.frame.height * 0.6)
}

func textFieldDidEndEditing(_ textField: UITextField)
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

self.view.endEditing(true);
}

更新了代码但仍然报错:

 class StudentSignUpViewController: UIViewController,UIScrollViewDelegate, UITextFieldDelegate {


@IBOutlet weak var yourEmail: UITextField!
@IBOutlet weak var yourPassword: UITextField!
@IBOutlet weak var joinUsButton: UIButton!
@IBOutlet weak var back2SignInPage: UIButton!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

//setting portrait
AppUtility.lockOrientation(.portrait)

//hide keyboard when click outside
self.hideKeyboardWhenTappedAround()

//hide keyboard when click on return
self.yourEmail.delegate = self
self.yourPassword.delegate = self

self.scrollView.delegate = self

//boarder line for nameText

//boarder line for yourEmail
yourEmail.frame.size.height = UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.05)
yourEmail.font = UIFont.italicSystemFont(ofSize: UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.04))
bottomBoader(BottomLine: "UrEmailTextBottomLine", length: 1.0, yourTextBox: yourEmail)
yourEmail.applyCustomClearButton(yourTextBox: yourEmail)

//boarder line for yourPassword
yourPassword.frame.size.height = UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.05)
yourPassword.font = UIFont.italicSystemFont(ofSize: UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.04))
bottomBoader(BottomLine: "UrPasswordTextBottomLine", length: 1.0, yourTextBox: yourPassword)
yourPassword.applyCustomClearButton(yourTextBox: yourPassword)

joinUsButton.layer.cornerRadius = joinUsButton.frame.height * 0.15
back2SignInPage.titleLabel?.font = UIFont.systemFont(ofSize: UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.03))


keyboardHeightNotification()
//textFieldDidBeginEditing(yourPassword)
textFieldDidEndEditing(yourPassword)


}


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

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
keyboardHeightNotification()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

AppUtility.lockOrientation(.portrait)
keyboardHeightNotification()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

AppUtility.lockOrientation(.all)
}




// ********************************** move text up when keyboard present ****************************************

var kbHeight: CGFloat?

func keyboardHeightNotification(){
NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil
)
}


@objc func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
kbHeight = keyboardSize.height
}

func textFieldDidBeginEditing(_ textField: UITextField) {

if textField.frame.maxY > kbHeight!
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - (kbHeight! + 2.0)), animated: true)

}

else{
return
}

print(textField.frame.maxY)
print(self.view.frame.height * 0.6)
print(textField.frame.maxY - self.view.frame.height * 0.6)
}
}



}




func textFieldDidEndEditing(_ textField: UITextField)
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

self.view.endEditing(true);
}


}

最佳答案

您可以为此使用第 3 方库,我经常使用它,称为 Typist link使用起来真的很简单,你可以得到很多关于键盘的信息

要像这样使用框架:

func startKeyboardListener() {
键盘
.on(event: .didShow) { [weak self] (options) in
self.setPosition(for:options.endFrame.origin.y)
}
。开始()
}

options.endFrame 是您想要的键盘框架。那里有很多更有用的信息

关于ios - 如何计算和使用 "textFieldDidBeginEditing"中的键盘高度(快速),以便我可以精确地将 UITextfield 向上移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47378057/

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