Hide keyboard when scroll UITableView

前端 未结 8 2223
借酒劲吻你
借酒劲吻你 2020-12-02 05:47

In my app i want hide keyboard when i start scrolling UITableView. I search about this in internet, and most answer is subclassing UITableView (http://stackoverflow.com/ques

8条回答
  •  [愿得一人]
    2020-12-02 06:03

    Task

    Hide keyboard programmatically when scroll UITableView in Swift 3

    Details

    xCode 8.2.1, swift 3

    Solution

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !tableView.isDecelerating {
            view.endEditing(true)
        }
    }
    

    Full Sample

    ViewController

    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var tableView: UITableView!
        @IBOutlet weak var searchBar: UISearchBar!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.dataSource = self
            tableView.delegate = self
        }
    }
    
    // MARK: - UITableViewDataSource
    
    extension ViewController: UITableViewDataSource {
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 100
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell =  UITableViewCell(style: .subtitle, reuseIdentifier: nil)
            cell.textLabel?.text = "Title"
            cell.detailTextLabel?.text = "\(indexPath)"
            return cell
        }
    }
    
    // MARK: - UITableViewDelegate
    
    extension ViewController: UITableViewDelegate {
    
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if !tableView.isDecelerating {
                view.endEditing(true)
            }
        }
    }
    

    StoryBoard

    
    
        
            
        
        
            
            
            
        
        
            
            
                
                    
                        
                            
                            
                        
                        
                            
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                    
                                
                            
                            
                            
                                
                                
                                
                                
                                
                                
                                
                            
                        
                        
                            
                            
                        
                    
                    
                
                
            
        
    
    

    Result

提交回复
热议问题