Swift - Move UItextField up when it is inside a UITableViewCell

0

Hello

I'm trying to move up a UITextField when it's inside a UITableViewCell and the keyboard appears, but I do not have a TableViewController . The UITableView is within a% regular%.

The code below works well for ViewController that is not within a UITextFields . When I use for UITableViewCell within a UITextFields it does not work. UITableViewCell does not move.

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
    }
}
    
asked by anonymous 24.05.2016 / 22:03

1 answer

0

In the textFieldShouldBeginEditing textField delegate, you can use the function below:

[self.scrollView setContentOffset:CGPointMake(0,textField.center.y-TSIZE) animated:YES];

The TSIZE can be adjusted according to your needs. If you are using a tableView you can replace the scrollView with tableView. I hope this helps.

    
25.05.2016 / 16:35