UITextField - 返回时向下滚动

时间:2018-04-02 04:33:04

标签: ios swift xcode uitextfield swift4

我在视图控制器上有多个文本字段,并且已经实现了textFieldShouldReturn函数,这样当我点击返回时,它会转到下一个文本字段。但是,现在屏幕保持不变,因此活动文本字段会被键盘覆盖。

我怎样才能使屏幕向下滚动,我可以看到活动文本字段?

相关代码:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        let nextTextFieldTag = textField.tag + 1
        let nextTextField = textField.superview?.viewWithTag(nextTextFieldTag)

        if nextTextField != nil {
            nextTextField?.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }

        return false
    }

5 个答案:

答案 0 :(得分:0)

最简单的方法是使用IQKeyboardManager(https://github.com/hackiftekhar/IQKeyboardManager

您需要做的就是将IQKeyboardManager添加到您的Pod中,安装它并添加最初的代码。那就是

https://github.com/hackiftekhar/IQKeyboardManager复制 添加到您的pod文件

`pod 'IQKeyboardManagerSwift'`

AppDelegate.swift中,只需import IQKeyboardManagerSwift框架并启用IQKeyboardManager。

import IQKeyboardManagerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

      IQKeyboardManager.sharedManager().enable = true

      return true
    }
}

如果你想自己做。

  1. textFieldShouldReturn中,获取活动文本字段的框架
  2. 新view.frame.y - = activeTextField.frame.y + space
  3. 更改view.frame.y
  4. 或者您可以将所有文本字段添加到滚动视图中,只需更改scrollview.contentOffset.y

答案 1 :(得分:0)

在UIScrollView中有两种滚动方式:

  • scrollView.setContentOffset(textField.frame.origin, animated: true)
  • scrollView.scrollRectToVisible(textField.frame, animated: true)

答案 2 :(得分:0)

我假设滚动视图scrollView上的所有文本字段。现在,您希望下一个文本字段nextTextField向上滚动。在那种情况下 -

if条件 -

scrollView.contentOffset = CGPoint(x : 0, y : nextTextField.frame.y)

答案 3 :(得分:0)

试试这个

viewDidLoad()这样的

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

使用这些功能显示键盘显示时的UITextField

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        print(keyboardSize.height)
        if view.frame.origin.y >= 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
        print(keyboardSize.height)
        if view.frame.origin.y < 0 {
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}

答案 4 :(得分:0)

  1. 当您开始使用文本字段并且想要移动ScrollView时。

     weak var activeField:[UITextField]!       
     func textFieldDidBeginEditing(_ textField: UITextField) {
         activeField = textField 
         if let scroll = scrollVC
             {
                 scroll.keyboardDismissMode = 
                 UIScrollViewKeyboardDismissMode.interactive
                 scroll.setContentOffset(CGPoint.init(x: 0, y: 
                 self.activeField.frame.origin.y - 64), animated: true)
             }
            }
    
  2. 当您关闭键盘时,请使用返回或使用.becomeFirstResponder()

    func textFieldDidEndEditing(_ textField: UITextField) {
    if let scroll = scrollVC{
        scroll.setContentOffset(CGPoint.init(x: 0, y: activeField == 
        textField ? 0 : textField.frame.origin.y), animated: true)
    }
    
  3. 当我有2个或更多TextField时,我如何使用第一响应者的示例: 您应该在Array中添加所有UITextField。

      @objc func dismissKeyboard(){
        self.view.endEditing(true)
      }
    
      func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch activeField {            
        case arrTextFields[0]:
            arrTextFields[1].becomeFirstResponder()
        case arrTextFields[1]:
            arrTextFields[2].becomeFirstResponder()
        default:
            dismissKeyboard()
        }
        return true
    }