Swift - 当UItextField位于UITableViewCell内时将其移动

时间:2016-05-24 19:05:32

标签: swift uitableview uiscrollview keyboard uitextfield

当我在UITextField内时,我正试图向上移动UITableViewCell并出现键盘,但我没有TableViewControllerUITableView位于常规ViewController内。

以下代码适用于不在UITextFields内的UITableViewCell。当我在UITextFields内使用它UITableViewCell时,它无效。 UIScrollView甚至没有移动。

import UIKit

class CriarPerfilViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    //MARK: - Outlets
    @IBOutlet weak var fotoImageView: UIImageView!
    @IBOutlet weak var perfilTableView: UITableView!
    @IBOutlet weak var scrollView: UIScrollView!


    //MARK: - Propriedades
    let nomeTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

    //MARK: - Métodos reescritos da View
    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        navigationItem.hidesBackButton = true

        //Define delegates
        perfilTableView.delegate = self
        perfilTableView.dataSource = self

        //Observers para ajuste de teclado
        let centralNotificacoes = NSNotificationCenter.defaultCenter()
        centralNotificacoes.addObserver(self, selector: #selector(ViewController.ajusteParaTeclado(_:)), name: UIKeyboardWillHideNotification, object: nil)
        centralNotificacoes.addObserver(self, selector: #selector(ViewController.ajusteParaTeclado(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)
    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Métodos da Table view data source
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let paddingView : UIView = UIView(frame: CGRectMake(0, 0, 10, 10))

        if indexPath.section == 0 && indexPath.row == 0 {
            nomeTextField.frame.size = cell.frame.size
            nomeTextField.placeholder = "Name"

            nomeTextField.leftView = paddingView
            nomeTextField.leftViewMode = UITextFieldViewMode.Always

            cell.addSubview(nomeTextField)
        }

        return cell
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        switch(section) {
            case 0: return "Exibition name"
            default: fatalError("Unknown section")
        }
    }

    @IBAction func tapGestureRecognizer(sender: UITapGestureRecognizer) {

        view.endEditing(true)
    }

    //MARK: - Métodos
    func ajusteParaTeclado(notificacao: NSNotification) {

        let informacoesDoUsuario = notificacao.userInfo!

        let tecladoFrameFinalTela = (informacoesDoUsuario[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let tecladoFrameFinalView = view.convertRect(tecladoFrameFinalTela, fromView: view.window)

        if notificacao.name == UIKeyboardWillHideNotification {
            scrollView.contentInset = UIEdgeInsetsZero
        } else {
            scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: tecladoFrameFinalView.height + 10, right: 0)
        }

        scrollView.scrollIndicatorInsets = scrollView.contentInset
    }
}

0 个答案:

没有答案