iOS 는 UISearchController 를 사용 하여 애니메이션 문 제 를 해결 합 니 다.

6402 단어 iOS
첫 번 째 단계:searchBar Should BeginEditing 을 통 해 해결 하고 편집 할 때 cancel 글꼴 문제:
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        searchCurrentPage = 1
        searchListArray = listArray
        tableView.reloadData()
        
        searchBar.setShowsCancelButton(true, animated: true);
        let tempView = searchBar.subviews.first;
        for subView: UIView in (tempView?.subviews)!{
            if subView.isMember(of: NSClassFromString("UINavigationButton")!){
                let cancelButton: UIButton = subView as! UIButton;
                cancelButton.setTitle("  ", for: .normal);
                break;
            }
        }
        
        return true
    }

두 번 째 단계:searchBarSearchButton Clicked 를 통 해 검색 을 클릭 하여 네트워크 요청 을 수행 합 니 다.
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        if searchListArray.count > 0 {
            searchListArray.removeAll()
        }
        
        //       ,      
        self.tableView.reloadData()
    }

세 번 째 단계:검색 BarCancelButton Clicked 를 통 해 검색 상태 취소
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchController.isActive = false
        self.tableView.reloadData()
    }

네 번 째 단계:검색 상자,팝 업,사라 질 때 애니메이션 이 동기 화 되 지 않 는 문 제 를 해결 합 니 다.
func willPresentSearchController(_ searchController: UISearchController) {
        self.edgesForExtendedLayout = .bottom
    }
    func willDismissSearchController(_ searchController: UISearchController) {
        self.edgesForExtendedLayout = .bottom

    }

전체 의사 코드:

import UIKit

class OcssClientListVC: OcssBaseVC, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate {
    private let clientListIdentifier = "OcssClientListCell"
    private var tableView: UITableView!
    private var listArray = [[String: Any]]()
    private var currentPage = 1
    private var pageSize = 30
    private var isHidden = false
    
    private var searchCurrentPage = 1
    private var searchListArray = [[String: Any]]()
    
    private var searchController: UISearchController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "    "
        
        let search = UISearchController(searchResultsController: nil)
        search.dimsBackgroundDuringPresentation = false
        self.searchController = search
        search.searchBar.placeholder = "   ID  "
//        search.searchResultsUpdater = self
        search.searchBar.delegate = self
        search.delegate = self
        search.searchBar.sizeToFit()
        
    
        tableView = UITableView.init(frame: view.bounds, style: .plain)
        tableView.delegate = self
        tableView.backgroundColor = UIConstants.ColorFromRGB(245, 245, 245)
        tableView.dataSource = self
        tableView.register(OcssClientListCell.self, forCellReuseIdentifier: clientListIdentifier)
        tableView.separatorStyle = .none
        view.addSubview(tableView)
        tableView.tableFooterView = UIView()
       
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { [weak self] in
            if let strongSelf = self {
                strongSelf.getAgentList()
            }
        })
        
        tableView.tableHeaderView = search.searchBar
        
        self.definesPresentationContext = true
//        self.searchVC.nav = self.navigationController;
//        self.searchVC.searchBar = self.searchController.searchBar;
        
        getAgentList()
        
        moreButton.isHidden = false
        moreButton.setTitle("  ", for: .normal)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isTranslucent = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.navigationController?.navigationBar.isTranslucent = true
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.isActive {
            return searchListArray.count
        }
        return listArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100.0 * UIConstants.NewScale_iPhone6
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:clientListIdentifier, for: indexPath) as! OcssClientListCell
        cell.selectionStyle = .none
        if searchController.isActive {
            // cell   
        }else {
           // cell   
        }
        return cell
    }
    
  
    
    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        searchCurrentPage = 1
        searchListArray = listArray
        tableView.reloadData()
        
        searchBar.setShowsCancelButton(true, animated: true);
        let tempView = searchBar.subviews.first;
        for subView: UIView in (tempView?.subviews)!{
            if subView.isMember(of: NSClassFromString("UINavigationButton")!){
                let cancelButton: UIButton = subView as! UIButton;
                cancelButton.setTitle("  ", for: .normal);
                break;
            }
        }
        
        return true
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        if searchListArray.count > 0 {
            searchListArray.removeAll()
        }
        //     
        self.tableView.reloadData()
           
    }
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        self.tableView.mj_footer.isHidden = isHidden
        searchController.isActive = false
        self.tableView.reloadData()
    }
    
    func willPresentSearchController(_ searchController: UISearchController) {
        self.edgesForExtendedLayout = .bottom
    }
    func willDismissSearchController(_ searchController: UISearchController) {
        self.edgesForExtendedLayout = .bottom

    }
    
}

좋은 웹페이지 즐겨찾기