Olá!
Estou tentando mover para cima um UITextField
quando ele está dentro de uma UITableViewCell
e o teclado aparece, mas eu não tenho um TableViewController
. A UITableView
está dentro de um ViewController
regular.
O código abaixo funciona bem para UITextFields
que não está dentro de uma UITableViewCell
. Quando eu uso para UITextFields
dentro de uma UITableViewCell
ele não funciona. O UIScrollView
não se mexe.
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
}
}