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 ViewController
regular.
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
}
}
This solution makes mine
scrollView
continue climbing each time I click inside theTextField
.– GuiDupas