Quick scroll search bar

I am developing a social networking application for iOS

My ViewControllers is currently built into the NavigationController . On the screen of my news feed, I need to display the search bar when the user moves (and hides it when he picks it up), in which, if the user types something, the search results will be displayed on top of the news feed screen.

I tried to work with this, but I'm pretty new to iOS and still haven't been able to get this to work.

Any help would be appreciated and kept in mind that I had only been programming iOS for several weeks, so some of them would be helpful. Thanks!

+6
source share
1 answer

First, you need to implement a UISwipeGestureRecognizer

enables setup() in viewDidAppear

 func setup() { let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(down)) swipeDown.direction = .down let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(up)) swipeUp.direction = .up self.view.addGestureRecognizer(swipeDown) self.view.addGestureRecognizer(swipeUp) searchBar = UISearchBar(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: 40.0)) if let searchBar = searchBar { searchBar.backgroundColor = UIColor.red self.view.addSubview(searchBar) } } 

Then your two functions up and down

 func down(sender: UIGestureRecognizer) { print("down") //show bar UIView.animate(withDuration: 1.0, animations: { () -> Void in self.searchBar!.frame = CGRect(x: 0.0, y: 64.0, width: self.view.frame.width, height: 40.0) }, completion: { (Bool) -> Void in }) } func up(sender: UIGestureRecognizer) { print("up") UIView.animate(withDuration: 1.0, animations: { () -> Void in self.searchBar!.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 40.0) }, completion: { (Bool) -> Void in }) } 

You can add Bool isShowing to avoid unnecessary animations. Then, run the textDidChange search bar delegate to change the search results by user type.

 func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String)` 

All you have to do is show the results in the UISearchController .

Note Using up / down scrolling may interfere with UIScreachController scrolling UIScreachController

+3
source

Source: https://habr.com/ru/post/977531/


All Articles