简体   繁体   English

键盘出现时如何对UIView进行排序?

[英]How to sort UIView when keyboard appears?

I can't find a good solution of how to move UIView up when the keyboard appears. 我找不到如何在出现键盘时向上移动UIView的好的解决方案。 My solution now is this: 我现在的解决方案是这样的:

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
    }


}

However, the problem with this is that it hides some of my boxes and a label and I can't see them. 但是,这样做的问题是它隐藏了我的一些盒子和标签,而我看不到它们。 I thik I would be able to avoid it with UIScrollView but I have no idea how to do it... Would appreciate any help! 我想我可以使用UIScrollView避免它,但是我不知道该怎么做...不胜感激!

You can use UITableView, then you can focus on the selected row using: 您可以使用UITableView,然后可以使用以下方法专注于所选行:

scrollToRow(at:at:animated:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM