키보드가 보이지 않을 때보기가 키보드 위이지만 테이블 아래쪽에 놓 이도록하는 예입니다.
빨간색 UIView가 추가되었습니다. 탭하면 키보드가 표시되어 키보드 위의 위치로 움직이는 것을 볼 수 있습니다. 셀 자체를 탭하면 키보드가 닫히고보기가 표의 맨 아래로 다시 이동하는 것을 볼 수 있습니다.
키보드 액세서리보기 및 하단에 첨부 된보기를 사용하는 등의 다른 방법이 있습니다. 그러나, 나는 그것에 들어 가지 않을 것이다.
예 사용하여 키보드 알림 :
//
// ViewController.swift
// SO
//
// Created by Brandon T on 2017-02-03.
// Copyright © 2017 XIO. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var table: UITableView!
var tableFooter: UIView!
var bottomConstraint: NSLayoutConstraint!
var dummyTextField: UITextField?
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
//Notifications
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
//Setup Views
self.table = UITableView(frame: self.view.frame, style: .grouped)
self.table.delegate = self
self.table.dataSource = self
self.tableFooter = UIView()
self.tableFooter.backgroundColor = UIColor.red
let recognizer = UITapGestureRecognizer(target: self, action: #selector(onViewTap(recognizer:)))
recognizer.numberOfTapsRequired = 1
recognizer.numberOfTouchesRequired = 1
self.tableFooter.addGestureRecognizer(recognizer)
//Constraints
self.view.addSubview(self.table)
self.view.addSubview(self.tableFooter)
self.table.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.table.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
self.table.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
self.tableFooter.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.tableFooter.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
self.tableFooter.topAnchor.constraint(equalTo: self.table.bottomAnchor).isActive = true
self.tableFooter.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
self.bottomConstraint = self.tableFooter.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
self.bottomConstraint.isActive = true
self.table.translatesAutoresizingMaskIntoConstraints = false
self.tableFooter.translatesAutoresizingMaskIntoConstraints = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func keyboardWillShow(notification: Notification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
self.view.layoutIfNeeded()
UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
if (self.bottomConstraint.constant <= 0.0) {
self.bottomConstraint.constant = -endFrame!.size.height
}
self.view.layoutIfNeeded()
}, completion: { (finished) in
})
}
}
func keyboardWillHide(notification: Notification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
self.view.layoutIfNeeded()
UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
self.bottomConstraint.constant = 0.0
self.view.layoutIfNeeded()
}, completion: { (finished) in
})
}
}
//TableView
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.00001
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.00001
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
if (cell == nil) {
cell = UITableViewCell(style: .default, reuseIdentifier: "cellID")
cell!.selectionStyle = .none
self.dummyTextField = UITextField(frame: CGRect(x: 100, y: 25.0, width: 100, height: 50))
cell!.contentView.addSubview(self.dummyTextField!)
}
cell!.backgroundColor = UIColor.white
cell!.textLabel?.text = "Test - \(indexPath.row)"
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.dummyTextField!.resignFirstResponder()
self.dummyTextField!.endEditing(true)
self.view.resignFirstResponder()
self.view.endEditing(true)
}
func onViewTap(recognizer: UITapGestureRecognizer) {
self.dummyTextField?.becomeFirstResponder()
}
}
알림 키보드 위의 "추가"버튼을 보여주기 위해 키보드 액세서리를 사용하고 있습니다. 대부분 키보드 알림을 듣고 숨기고 테이블 바닥 글을 보여줍니다. 테이블을 키보드로 프레임을 조정하도록 할 수도 있습니다. 실제로 바닥 글 인 경우 바닥 글이 위로 이동합니다. 특별한 것은 없습니다. – Brandon