我找不到一个很好的解决方案,当键盘出现时如何向上移动UIView。我现在的解决办法是:

import UIKit

class LoginViewControllerbybys: UIViewController, UITextFieldDelegate {

    // Create username and password text boxes
    let emailTextField: UITextField = {
        let field = UITextField()
        field.placeholder = "EMAIL"
        field.font = UIFont(name: "Helvetica Neue", size: 14)
        field.background = UIImage(named: "divider")
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.spellCheckingType = .no
        field.translatesAutoresizingMaskIntoConstraints = false
        return field
    }()

    let passwordTextField: UITextField = {
        let field = UITextField()
        field.placeholder = "PASSWORD"
        field.font = UIFont(name: "Helvetica Neue", size: 14)
        field.background = UIImage(named: "divider")
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.spellCheckingType = .no
        field.isSecureTextEntry = true
        field.translatesAutoresizingMaskIntoConstraints = false
        return field
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white // sets background colour to white

        //Adding notifies on keyboard appearing
        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)

        // Adding Logo in the centre-top of the screen
        let logo: UIImageView = {
            let image = UIImage(named: "logo")
            let imageView = UIImageView(image: image)
            imageView.translatesAutoresizingMaskIntoConstraints = false
            return imageView
        }()

        func logoView() {
            logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            logo.heightAnchor.constraint(equalToConstant: 75).isActive = true
            logo.widthAnchor.constraint(equalToConstant: 75).isActive = true
            logo.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
        }

        func emailTextFieldView() {
            emailTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            emailTextField.heightAnchor.constraint(equalToConstant: 35).isActive = true
            emailTextField.widthAnchor.constraint(equalToConstant: 310).isActive = true
            emailTextField.topAnchor.constraint(equalTo: logo.bottomAnchor, constant: 50).isActive = true
        }

        func passwordTextFieldView() {
            passwordTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            passwordTextField.heightAnchor.constraint(equalToConstant: 35).isActive = true
            passwordTextField.widthAnchor.constraint(equalToConstant: 310).isActive = true
            passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 30).isActive = true
        }

        // Create Sign In button
        let signInButton: UIButton = {
            let button = UIButton()
            button.setImage(#imageLiteral(resourceName: "goButton"), for: .normal)
            button.frame = CGRect(x: 0, y: 597, width: 375, height: 70)
            button.addTarget(self, action: #selector(signInButtonTapped), for: .touchUpInside) // when tapped on Sign In button, execute function SignInTapped
            button.translatesAutoresizingMaskIntoConstraints = false
            return button
        }()


        func signInButtonView() {
            signInButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            signInButton.heightAnchor.constraint(equalToConstant: 70).isActive = true
            signInButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
            signInButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        }

        // Show all Subviews
        self.view.addSubview(logo)
        self.view.addSubview(signInButton)
        self.view.addSubview(emailTextField)
        self.view.addSubview(passwordTextField)

        // Constraints
        logoView()
        signInButtonView()
        emailTextFieldView()
        passwordTextFieldView()
    }

    func keyboardWillShow(notification: NSNotification) {

        let keyboardSize: CGSize = ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!
        let offset: CGSize = ((notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size)!



        if keyboardSize.height == offset.height {
            if self.view.frame.origin.y == 0 {
                UIView.animate(withDuration: 0.1, animations: { () -> Void in
                    self.view.frame.origin.y -= keyboardSize.height
                })
            }
        } else {
            UIView.animate(withDuration: 0.1, animations: { () -> Void in
                self.view.frame.origin.y += keyboardSize.height - offset.height
            })
        }
    }

    func keyboardWillHide(notification: NSNotification) {
        let keyboardSize: CGSize = ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!
        self.view.frame.origin.y += keyboardSize.height
    }


}

但是,问题是它隐藏了我的一些盒子和标签,我看不见它们。我想我可以用UIScrollView来避免它,但是我不知道怎么做。。。如果有任何帮助,我将不胜感激!

最佳答案

您可以使用UITableView,然后可以使用以下命令聚焦选定行:

scrollToRow(at:at:animated:)

https://developer.apple.com/reference/uikit/uitableview/1614997-scrolltorow

10-08 06:09