gpt4 book ai didi

ios - 当键盘可见时,ScrollView 不会自动向上移动

转载 作者:行者123 更新时间:2023-11-30 12:54:51 28 4
gpt4 key购买 nike

当键盘出现在屏幕上时,我在向上移动 ScrollView 时遇到问题。 我的屏幕上有 4 个文本字段,它们都是数字键盘文本字段,没有下一个/完成返回键,因此我在键盘上添加了带有上一个和下一个按钮的自定义工具栏。 因此,当我使用下一个按钮从一个文本字段移动到另一个文本字段时,光标会正确移动到下一个字段,但 ScrollView 不会向上移动,因此我的文本字段隐藏在键盘后面。我已将 ScrollView 添加到我的 Storyboard中,并设置了文本字段的委托(delegate)。

代码:

override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.registerForKeyboardNotifications()
keyboardVisible = false
}


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

self.deregisterFromKeyboardNotifications()

}



func registerForKeyboardNotifications ()-> Void {

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillBeHidden(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

func deregisterFromKeyboardNotifications () -> Void {
let center: NotificationCenter = NotificationCenter.default
center.removeObserver(self, name: NSNotification.Name.UIKeyboardDidShow, object: nil)
center.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)


}





func keyboardWasShown (_ notification: Notification) {
if(keyboardVisible == true){
return
}

print(scrollView)
if let keyboardSize = ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

offset = scrollView!.contentOffset
print(offset)

var viewFrame = scrollView!.frame
viewFrame.size.height -= keyboardSize.height
scrollView!.frame = viewFrame

var textFieldRect = activeField?.frame
textFieldRect?.origin.y += 10;
scrollView!.scrollRectToVisible(textFieldRect!, animated: true)

keyboardVisible = true;


}


}

func keyboardWillBeHidden (_ notification: Notification) {

if(keyboardVisible == false){
return;
}
scrollView.frame = CGRect(x: 0, y: 0, width: scrollViewWidth, height: scrollViewHeight)
scrollView?.contentOffset = offset

keyboardVisible = false


}


// textfield delegate

//in textfieldDidBeginEditing i m setting custom toolbar on textfield input accesory view
open func textFieldDidBeginEditing(_ textField: UITextField) {
if(textField == myTextField1){
myTextField1.inputAccessoryView = myCustomToolbar
myCustomToolbar?.nextButton.isEnabled = true
myCustomToolbar?.previousButton.isEnabled = false
myCustomToolbar?.nextButton.title = "Next"
}else if(textField == myTextField2){
myTextField2.inputAccessoryView = myCustomToolbar
myCustomToolbar?.previousButton.isEnabled = true
myCustomToolbar?.nextButton.isEnabled = true
myCustomToolbar?.nextButton.title = "Next"
}else if(textField == myTextField3){
myTextField3.inputAccessoryView = myCustomToolbar
myCustomToolbar?.nextButton.isEnabled = true
myCustomToolbar?.previousButton.isEnabled = true
myCustomToolbar?.nextButton.title = "Next"
}else if(textField == myTextField4){
myTextField4.inputAccessoryView = myCustomToolbar
myCustomToolbar?.nextButton.isEnabled = true
myCustomToolbar?.previousButton.isEnabled = true
myCustomToolbar?.changeNextToActionButton(withTitle: "Some title")
}
}


open func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
activeField = textField
return true
}


//PnPToolbar Delegate

public func pnPToolbarNextButtonClicked(_ myCustomToolbar: myCustomToolbar!) {
if(myTextField1.isFirstResponder){
myTextField2.becomeFirstResponder()
}else if(myTextField2.isFirstResponder){
myTextField3.becomeFirstResponder()
}else if(myTextField3.isFirstResponder){
myTextField4.becomeFirstResponder()

}else if(ifscCode.isFirstResponder){
//Done call your method
self.myMethod()
}
var textFieldRect = activeField?.frame
textFieldRect?.origin.y += 10;
scrollView!.scrollRectToVisible(textFieldRect!, animated: true)
}

public func myCustomToolbarPreviousButtonClicked(_ myCustomToolbar: myCustomToolbar!) {
if(myTextField4.isFirstResponder){
myTextField3.becomeFirstResponder()
}else if(myTextField3.isFirstResponder){
myTextField2.becomeFirstResponder()
}else if(myTextField2.isFirstResponder){
myTextField1.becomeFirstResponder()
}
var textFieldRect = activeField?.frame
textFieldRect?.origin.y += 10;
scrollView!.scrollRectToVisible(textFieldRect!, animated: true)

}

当我从 myTextField1 移动到 myTextField2 时,光标到达第二个文本字段,但 ScrollView 不会向上移动, ScrollView 的内容大小也不会向上移动到键盘之外。

最佳答案

在 TextField 的委托(delegate)中管理您的标志,如下所示:

 func textFieldDidBeginEditing(_ textField: UITextField) {
keyboardVisible = true
}

func textFieldEndEditings(sender:UITextField) -> Void {
print(sender.text)
keyboardVisible = false
}

关于ios - 当键盘可见时,ScrollView 不会自动向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542887/

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