Swift-teaches you how to implement UISearchController animation effects on navigation .,
This code snippet shows that I found various materials from the internet this week and modified it to get the upper and lower animation effects of the navigation:
Step 1: => because this search requires an animation effect, the page must have a navigation controller:
// 1. Create a custom navigation Controller
I jumped to this page from other pages. Before the jump, I customized a navigation controller:
Let actionSearchTable = searchTable ();
Let navVC = UINavigationController (rootViewController: actionSearchTable );
NavVC. navigationBar. barStyle = UIBarStyle. BlackTranslucent;
Self. presentViewController (navVC, animated: true, completion: nil );
// 2. click search to jump to searchTable. swift: This page inherits UITableViewController instead of UiViewController. You must pay attention to it. Otherwise, when you click a search to cancel a search, the table will be blank. I am not very clear about this principle, if you find out the principles, I hope you can give me some advice.
The data source of this table is referenced by a txt file. Format:
/// SearchResultTable. swift // search box /// Created by Lu Yang on 15/11/6. // Copyright©November 2015. all rights reserved. // import UIKitclass searchTable: UITableViewController, UISearchBarDelegate {lazy var dismissBtn: UIButton = {UIButton (frame: CGRectMake (0, 0, 24, 24 ))}(); // return button var itemsString: [String]! Var searcher: UISearchController! Var searchBars: UISearchBar? Struct SearchControllerRestorableState {var wasActive = false var wasFirstResponder = false} var restoredState = SearchControllerRestorableState (); // The initialization function override func viewDidLoad () {super. viewDidLoad (); self. title = ""; initView () ;}// initialize UI func initView () {dismissBtnPrepare (); // CREATE Table self. tableView = UITableView (frame: CGRectMake (0, 80, UIScreen. mainScreen (). bounds. width, UIScreen. mainS Creen (). bounds. height); self. tableView. delegate = self; self. tableView. dataSource = self; self. tableView. registerClass (UITableViewCell. self, forCellReuseIdentifier: "cells") // 1. read table data let tablePath = NSBundle. mainBundle (). pathForResource ("states", ofType: "txt ")!; Let tableData = try! NSString (contentsOfFile: tablePath, encoding: NSUTF8StringEncoding); itemsString = tableData. componentsSeparatedByString ("\ n") as [String]; let src = searchResultTable (data: itemsString) searcher = UISearchController (searchResultsController: src) searcher. searchResultsUpdater = src; // shadow effect searcher is displayed when the focus is obtained. dimsBackgroundDuringPresentation = true; // retrieves the animation effect searcher from the focus navigation up. hidesNavigationBarDuringPrese Ntation = true; searchBars = searcher. searchBar tableView. tableHeaderView = searchBars ?. Delegate = self; searchBars ?. Placeholder = "enter Merchant name"; // cancel button color and text box cursor color searchBars ?. TintColor = UIColor. blueWithTabbar (); // background color of the search box // searchBars ?. BarTintColor = UIColor. blackColor (); searcher. searchBar. sizeToFit (); self. tableView. reloadData ();
// The background is filled with navigation definesPresentationContext = true;} override func viewDidAppear (animated: Bool) {super. viewDidAppear (animated) // Restore the searchController's active state. if restoredState. wasActive {searcher. active = restoredState. wasActive restoredState. wasActive = false if restoredState. wasFirstResponder {searcher. searchBar. becomeFirstResponder () restoredState. wasFirstResponder = false }}} ove Rride func viewDidDisappear (animated: Bool) {super. viewDidAppear (animated); // 2.3 changes the status bar to white UIApplication. sharedApplication (). statusBarStyle = UIStatusBarStyle. lightContent;} func searchBarSearchButtonClicked (searchBar: UISearchBar) {searchBar. resignFirstResponder ()} override func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {return itemsString. count} override f Unc tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {let cell = tableView. Convert ("cells", forIndexPath: indexPath) cell. textLabel !. Text = itemsString [indexPath. row]; return cell} override func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {// cancel the selected style tableView. deselectRowAtIndexPath (indexPath, animated: true); // obtain the row index if (indexPath. row = 0) {}}/** return button */func dismissBtnPrepare () {dismissBtn. setImage (UIImage (named: "img. bundle/cancel "), forState: UIControlState. normal) dismissBtn. addTarget (self, action: "dismissLogin", forControlEvents: UIControlEvents. touchUpInside) self. navigationItem. leftBarButtonItem = UIBarButtonItem (customView: dismissBtn)}/** release the current page */func dismissLogin () {self. dismissViewControllerAnimated (true, completion: nil)} func searchBarCancelButtonClicked (searchBar: UISearchBar) {print ("B"); // 2.3 changes the status bar to white UIApplication. sharedApplication (). statusBarStyle = UIStatusBarStyle. lightContent;} // the search box starts to edit the event func searchBarTextDidBeginEditing (searchBar: UISearchBar) {// 2.3 changes the status bar to white UIApplication. sharedApplication (). statusBarStyle = UIStatusBarStyle. default;} func searchBarTextDidEndEditing (searchBar: UISearchBar) {// 2.3 changes the status bar to white UIApplication. sharedApplication (). statusBarStyle = UIStatusBarStyle. lightContent ;}}
// 3. searchResultTable. swift
/// SearchResultTable2.swift // searchBarDemo /// Created by Lu Yang on 15/11/6. // Copyright©November 2015: nevenmoore. All rights reserved. // import Foundationimport UIKitclass searchResultTable: UITableViewController, UISearchResultsUpdating {var tableData: UITableView !; Var originalData: [String]! // Original data var filteredData: [String]! // Filter data override func viewDidLoad () {super. viewDidLoad (); self. title = "enter Merchant name"; initView ()} init (data: [String]) {originalData = data super. init (nibName: nil, bundle: nil)} required init? (Coder aDecoder: NSCoder) {fatalError ("init (coder :) has not been implemented")} // initialize UI func initView () {// CREATE Table tableData = UITableView (frame: CGRectMake (0, 0, UIScreen. mainScreen (). bounds. width, UIScreen. mainScreen (). bounds. height); self. view. addSubview (tableData); // tableData. backgroundColor = UIColor. redColor (); tableData. delegate = self; tableData. dataSource = self; tableData. registerClass (UITabl EViewCell. self, forCellReuseIdentifier: "cells")} override func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {// # warning Incomplete implementation, return the number of rows return filteredData. count} override func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {let cell = tableView. dequeueReusableCellWithIden Tifier ("cells", forIndexPath: indexPath) cell. textLabel !. Text = originalData [indexPath. row]; return cell} func updateSearchResultsForSearchController (searchController: UISearchController) {let searchBar = searchController. searchBar; let target = searchBar. text; filteredData = originalData. filter () {s in var options = NSStringCompareOptions. caseInsensitiveSearch if searchController. searchBar. selectedScopeButtonIndex = 0 {options = options. union (. anchored Search)} let found = s. rangeOfString (target !, Options: options) return (found! = Nil)} tableData. reloadData ()} override func didReceiveMemoryWarning () {super. didreceivemorywarning ();}}
Well, the final result is as follows: But there is a little problem with the character string filtering, so please let me know if the problem is solved ..