[Swift]键盘遮挡

键盘遮挡控件:

 1  super.viewDidLoad(){
 2      // Do any additional setup after loading the view, typically from a nib.
 3      //监听键盘弹出通知
 4      NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillShow(_:)), name:UIResponder.keyboardWillShowNotification, object: nil)
 5      //监听键盘隐藏通知
 6      NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillHide(_:)), name:UIResponder.keyboardWillHideNotification, object: nil)
 7  }
 8 
 9 //键盘显示
10 @objc func keyboardWillShow(_ notification: Notification) {
11     let userInfo = (notification as NSNotification).userInfo!
12     //键盘尺寸
13     let keyboardSize = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
14     //keyboardSize.height
15     //keyboardSize.width
16     
17 }
18 
19 //键盘隐藏
20 @objc func keyboardWillHide(_ notification: Notification) {
21     //还原控件位置
22     
23 }

tableView 所在的视图控制器是 UITableViewController ,则不需要特别处理即可放心使用,系统会自动处理键盘遮挡问题。

tableView 所在的视图控制器是 UIViewController,这里有两种办法防止键盘遮挡。

  • 方法1:在原来的 UIViewController 内部再添加一层 UITableViewController
 1 import UIKit
 2  
 3 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 4      
 5     var tableView:UITableView?
 6      
 7     override func loadView() {
 8         super.loadView()
 9     }
10      
11     override func viewDidLoad() {
12         super.viewDidLoad()
13          
14         //创建表视图
15         self.tableView = UITableView(frame: self.view.frame, style:.plain)
16         self.tableView!.delegate = self
17         self.tableView!.dataSource = self
18         //创建一个重用的单元格
19         self.tableView!.register(MyTextFieldCell.self, forCellReuseIdentifier: "tableCell")
20         self.view.addSubview(self.tableView!)
21          
22         //添加一个UITableViewController
23         let tableVC = UITableViewController.init(style: .plain)
24         tableVC.tableView = self.tableView
25         self.addChildViewController(tableVC)
26     }
27      
28     //返回表格行数(也就是返回控件数)
29     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
30         return 20
31     }
32      
33     //创建各单元显示内容(创建参数indexPath指定的单元)
34     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
35         -> UITableViewCell {
36             //创建一个重用的单元格
37             let cell = tableView
38                 .dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
39                 as! MyTextFieldCell
40              
41             cell.label.text = "条目\(indexPath.row)"
42             return cell
43     }
44  
45     override func didReceiveMemoryWarning() {
46         super.didReceiveMemoryWarning()
47     }
48 }
  • 方法2:监听键盘通知,在键盘出现或消失的时候修改tableView 的 contentInset 和 scrollIndicatorInsets
 1 import UIKit
 2  
 3 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 4      
 5     var tableView:UITableView?
 6      
 7     override func loadView() {
 8         super.loadView()
 9     }
10      
11     override func viewDidLoad() {
12         super.viewDidLoad()
13          
14         //创建表视图
15         self.tableView = UITableView(frame: self.view.frame, style:.plain)
16         self.tableView!.delegate = self
17         self.tableView!.dataSource = self
18         //创建一个重用的单元格
19         self.tableView!.register(MyTextFieldCell.self, forCellReuseIdentifier: "tableCell")
20         self.view.addSubview(self.tableView!)
21          
22         //监听键盘弹出通知
23         NotificationCenter.default
24             .addObserver(self,selector: #selector(keyboardWillShow(_:)),
25                          name: NSNotification.Name.UIKeyboardWillShow, object: nil)
26         //监听键盘隐藏通知
27         NotificationCenter.default
28             .addObserver(self,selector: #selector(keyboardWillHide(_:)),
29                          name: NSNotification.Name.UIKeyboardWillHide, object: nil)
30     }
31      
32     //返回表格行数(也就是返回控件数)
33     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
34         return 20
35     }
36      
37     //创建各单元显示内容(创建参数indexPath指定的单元)
38     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
39         -> UITableViewCell {
40             //创建一个重用的单元格
41             let cell = tableView
42                 .dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
43                 as! MyTextFieldCell
44              
45             cell.label.text = "条目\(indexPath.row)"
46             return cell
47     }
48      
49     // 键盘显示
50     func keyboardWillShow(_ notification: Notification) {
51         let userInfo = (notification as NSNotification).userInfo!
52         //键盘尺寸
53         let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey]
54             as! NSValue).cgRectValue
55         var contentInsets:UIEdgeInsets
56         //判断是横屏还是竖屏
57         let statusBarOrientation = UIApplication.shared.statusBarOrientation
58         if UIInterfaceOrientationIsPortrait(statusBarOrientation) {
59             contentInsets = UIEdgeInsetsMake(64.0, 0.0, (keyboardSize.height), 0.0);
60         } else {
61             contentInsets = UIEdgeInsetsMake(64.0, 0.0, (keyboardSize.width), 0.0);
62         }
63         //tableview的contentview的底部大小
64         self.tableView!.contentInset = contentInsets;
65         self.tableView!.scrollIndicatorInsets = contentInsets;
66     }
67      
68     // 键盘隐藏
69     func keyboardWillHide(_ notification: Notification) {
70         //还原tableview的contentview大小
71         let contentInsets:UIEdgeInsets = UIEdgeInsetsMake(64.0, 0.0, 0, 0.0);
72         self.tableView!.contentInset = contentInsets
73         self.tableView!.scrollIndicatorInsets = contentInsets
74     }
75      
76     override func didReceiveMemoryWarning() {
77         super.didReceiveMemoryWarning()
78     }
79 }

 

上一篇:anyRTC2020年 年终总结


下一篇:IOS tableview 使用