Last active
May 30, 2019 12:17
-
-
Save takashi1975/8d31a7eb47ca5d46d7b6859df5004334 to your computer and use it in GitHub Desktop.
Swift4 カスタムセルのUITableViewの中のTextField がキーボードに隠れないようにしたい... (暫定処置)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* UITableView を参照できるようにしておく | |
* deinit で 通知の解除 をしておく | |
*/ | |
class ViewController: UIViewController { | |
// TableView | |
@IBOutlet weak var tableView: UITableView! | |
// UITextField (Keyboardで隠さないよう..) | |
var activeTextFiled: UITextField? = nil | |
//... | |
//deinit class ないしか実装できない... | |
deinit { | |
let center = NotificationCenter.default | |
//解除 | |
center.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) | |
center.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* キーボードを表示・閉じるタイミングを通知してもらう | |
* http://programming-beginner-memo.com/?p=427 | |
*/ | |
extension ViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// ... | |
//キーボードの状態監視 | |
if (true) { | |
let center = NotificationCenter.default | |
center.addObserver(self, | |
selector: #selector(ViewController.keyboardWillShow(_:)), | |
name: NSNotification.Name.UIKeyboardWillShow, | |
object: nil) | |
center.addObserver(self, | |
selector: #selector(ViewController.keyboardWillHide(_:)), | |
name: NSNotification.Name.UIKeyboardWillHide, | |
object: nil) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* カスタムセル の中の UITextField | |
* (xib で カスタムセル内の UITextField の delegate を ViewController に関連付けしておく) | |
* | |
* フォーカスが当たった時の UITextField を覚えておく (キーボード表示時に使用) | |
*/ | |
extension ViewController: UITextFieldDelegate { | |
//入力開始 (Editing Did Begin イベント) | |
@IBAction func didEditBeginForTextField(_ sender: Any) { | |
if let textField = sender as? UITextField { | |
self.activeTextFiled = textField | |
} | |
} | |
//フォーカスが外れた (Editing Did End イベント) | |
@IBAction func didEditEndTextField(_ sender: Any) { | |
if let textField = sender as? UITextField { | |
self.activeTextFiled = nil | |
} | |
} | |
//UITextFieldDelegate: 編集開始時 | |
internal func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { | |
//自分は Editint Did Begin - End で設定してみた。 | |
//self.activeTextFiled = textField | |
return true | |
} | |
//UITextFieldDelegate: キーボード内の Doneボタン を押した時 | |
internal func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
//キーボードをしまう | |
self.dismissKeyboard() | |
return true | |
} | |
//キーボードを閉じる | |
@objc private func dismissKeyboard() { | |
self.view.endEditing(true) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* キーボードを表示する時にUITextFieldと被っていないかチェック、(被っていれば、ずらす) | |
*/ | |
extension ViewController { | |
//キーボードを表示するとき | |
@objc func keyboardWillShow(_ notification: Notification) { | |
if let userInfo = notification.userInfo, | |
let tableView = self.tableView, | |
let cell = self.activeTextFiled?.superview?.superview as? CustomSettingCell { | |
//画面 | |
let boundSize: CGSize = UIScreen.main.bounds.size | |
//キーボード (上辺 ... 画面の高さからキーボードの高さを引いた値) | |
let keyboardScreenEndFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue | |
let keyboardLimit: CGFloat = boundSize.height - keyboardScreenEndFrame.size.height | |
//セルのスクリーン座標 (座標変換) | |
let cellFrame: CGRect = tableView.convert(cell.frame, to: nil) | |
let cellLimit: CGFloat = cellFrame.origin.y + cellFrame.height + 8.0 | |
//キーボードで隠れてしまう場合... | |
let offset: CGFloat = cellLimit - keyboardLimit | |
if (offset > 0) { | |
//TODO: (本当は...)キーボードの上にくるようにセルを移動させたい... | |
//暫定処置... (該当セルを画面の真ん中に移動させる) | |
if let indexPath = tableView.indexPath(for: cell) { | |
tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true) | |
} | |
} | |
} | |
} | |
//キーボードをしまうとき | |
@objc func keyboardWillHide(_ notification: Notification) { | |
//TODO: (本当は...)キーボードをしまう時に、移動した分 元の位置に戻したい... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment