编辑UITextField时正在重置UIScrollView

时间:2017-04-28 12:50:35

标签: ios iphone swift uiscrollview

我是Swift 3中的新手,我只是注意到我们需要滚动一个视图,因为当我们输入文本字段时键盘可以覆盖一些小部件,就像在此视频中一样。 Fixing UITextField-Keyboard Problems (Swift in Xcode)

但是,即使在以编程方式滚动视图之前,我也会遇到不幸的行为。我的观点一直被重置到其初始位置。我让video显示了这种行为。

我找不到这个问题的提及。有人会有线索吗?

[EDITED]

这是小部件的结构

enter image description here

这是我的View Controller代码:

import UIKit
import AVFoundation

class SignUpController: UIViewController, UITextFieldDelegate
{

    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var stackView: UIStackView!

    @IBOutlet var usernameTextField: UITextField!
    @IBOutlet var passwordTextField: UITextField!
    @IBOutlet var confirmationTextField: UITextField!
    @IBOutlet var emailTextField: UITextField!
    @IBOutlet var phoneTextField: UITextField!
    @IBOutlet var firstNameTextField: UITextField!
    @IBOutlet var lastNameTextField: UITextField!

    @IBOutlet var signUpButton: UIButton!

    var keyboardHeigh:CGFloat!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.scrollView.contentSize = self.stackView.frame.size
        self.scrollView.contentSize.height += self.stackView.frame.minY + 20
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let textFields :[UITextField] =
            [
                usernameTextField,
                passwordTextField,
                confirmationTextField,
                emailTextField,
                phoneTextField,
                firstNameTextField,
                lastNameTextField,
            ]

        for i in 0..<textFields.count
        {
            textFields[i].delegate = self
            textFields[i].tag = i
        }

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

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

    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {
        // Try to find next responder
        if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField
        {
            nextField.becomeFirstResponder()

        }
        else
        {
            // Not found, so remove keyboard.
            textField.resignFirstResponder()

        }

        return false
    }
}

1 个答案:

答案 0 :(得分:1)

1.您可以使用静态单元格

获取UItableViewController

2.在单元格内放置textField。

3.选择单元格无。

4.使分隔符无。

它会正常工作。