当键盘出现在屏幕上时,我在向上移动srollview时遇到问题。 我的屏幕上有4个文本字段,所有都是数字小键盘文本字段,没有下一个/完成返回键,所以我已经在键盘上添加了自定义工具栏,上一个和下一个按钮。 因此,当我使用下一个按钮从一个文本字段移动到另一个文本字段时,光标会正确移动到下一个字段,但是scrollview不会向上移动,因此我的文本字段隐藏在键盘后面。我已经将scrollview添加到我的故事板中,并设置了textfield的代表。
代码:
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时,光标进入第二个文本字段但滚动视图不会向上移动,滚动视图的内容大小也不会超出键盘。
答案 0 :(得分:0)
在TextField的委托中管理您的旗帜,如下所示:
func textFieldDidBeginEditing(_ textField: UITextField) {
keyboardVisible = true
}
func textFieldEndEditings(sender:UITextField) -> Void {
print(sender.text)
keyboardVisible = false
}
答案 1 :(得分:0)
对于键盘上方的工具栏
let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
numberToolbar.barStyle = UIBarStyle.Default
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelNumberPad"),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")]
numberToolbar.sizeToFit()
phonenumberTextField.inputAccessoryView = numberToolbar