Swift - Move Uitextfield up when it is inside a Uitableviewcell

Asked

Viewed 100 times

0

Hello!

I’m trying to move up a UITextField when he’s inside a UITableViewCell and the keyboard appears, but I don’t have a TableViewController. To UITableView is inside a ViewControllerregular.

The code below works well for UITextFields that is not inside a UITableViewCell. When I use to UITextFields within a UITableViewCell it does not work. The UIScrollView 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
    }
}

1 answer

0

In the delegate of the textFiled textFieldShouldBeginEditing, you can use the function below:

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

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

  • This solution makes mine scrollViewcontinue climbing each time I click inside the TextField.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.