UISearchBar disable animation

I have a UITableViewController with SearchBar and a search display controller:

enter image description here

Everything is default. The navigation bar is semi-transparent. When you click on the search bar, the navigation bar moves up. Now, when you dismiss the search, it goes backwards, which creates a white space between the search bar and the navigation:

enter image description here

Any ideas what is causing this and how to fix it? I am using swift, iOS8.1 and xcode6.1


The only thing I can do is set the background of the table view to blue. But this has the negative side effect that everything will be blue if the table view is empty.

+3


source to share


8 answers


From the code you posted on GitHub I could find a way to avoid the break, obviously a matter of animation timing.

In your FirstTableViewController.swift, you need to register yourself as a UISearchBar delegate, and then trigger yourself to show the navbar earlier when the searchBar rejects (as soon as text editing finishes) using the searchBarTextDidEndEditing delegate method:

Here is your last quick file with 2 additions: UISearchBarDelegate on line 3 and the searchBarTextDidEndEditing function at the end:

import UIKit

class FirstTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"];

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("test", sender: nil)
    }

    // Fix searchbar disappear bug

    func searchDisplayControllerDidEndSearch(controller: UISearchDisplayController) {
        self.tableView.insertSubview(self.searchDisplayController!.searchBar, aboveSubview: self.tableView)
    }


    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }


}

      



EDIT - By setting the correct layout parameters in IB, both image managers work.

First: The 3 checked options for TableViews are in Drawing: Opaque + Clears the Graphics Context + Autoresize Subviews

Second: for the ViewController itself, the Extended Edges options are: Under Bottom Bars + Under Opaque Bars

And third: the code above is about sharing UISearchBar ...

+5


source


Try to place your UISearchBar outside of your table view and not as a subhead. It might be a small design change, but the animation is much better.



+3


source


I believe the "white space" is the background color of the table controller. I suppose you can change the background color of the table controller to match the search bar or navigation bar, but then the entire table view controller will have that color as well.

+2


source


Change the search bar to gray (i.e. background color). This might help you

+2


source


white is the background color of your nav controller

1) in the storyboard, assign your nav controller class and set its background color to whatever you want ... as in my example I did in viewDidLoad ().

2) set backgroundColor as a table. by default it will be the same as the main background of the navigator controller (do this in storyboard ok)

class NavControllerViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.redColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

      

if you want to have the same color as the navigation bar

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.backgroundColor = navigationBar.tintColor
}

      

+2


source


The statusBar color is caused by the extendedLayoutIncludesOpaqueBars property . It is set to "Yes" by default. Set it to "No", then the status bar will always have the same color.

Hope this helps you.

+1


source


As per your question, I created a similar demo and concluded that you are getting a space between the nav and search bar because you have set a control bar to search instead of the search bar.

I did some more digging and found out that this is the default search behavior .

Whereas when using the Controller search bar, the appearance of the table also shows a space between the nav bar and the search bar.

Instead of using UISearchBarController, you need to use the UISearch bar in the UIView outside of the UItableView.

You can show and hide the navigation bar in a similar way with the search bar in the following method, using animation to hide and show the navigation bar.

EDIT:

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {

    UIView.animateWithDuration(0.3, animations: {
        searchBar.showsCancelButton = true
        self.navigationController?.navigationBarHidden = true
        }, completion: { value in
            //comment below lines is you want smooth animation.
            self.navigationController!.navigationBar.alpha = 0
    })
    return true
}

      


func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    UIView.animateWithDuration(0.3, animations: { () -> Void in
        searchBar.showsCancelButton = false
        searchBar.resignFirstResponder()
           // Uncomment below line for smooth animation effect.
           // self.navigationController!.navigationBarHidden = false
        }, completion: { finished in
            //comment below lines is you want smooth animation.
            self.navigationController!.navigationBar.alpha = 1
            self.navigationController!.navigationBarHidden = false
    })
}

      

Now you don't need to set the background color as the color of the navbar where the wall doesn't have a smooth animation like it used to when searchBarCancelButtonClicked .

But this won't have the smooth animation like it used to.
This way you can just set the background color as the color of the navigation bar.

Hope this solves your problem.

+1


source


import UIKit

class NavControllerViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.tintColor = UIColor.redColor()
        view.backgroundColor = view.tintColor
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}


class FirstTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"];

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("test", sender: nil)
    }

}

class SecondTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"]

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

}

      

there is no need to do any special tricks. as I wrote in my first answer, the white space is just the background color of the main NavigationController view.

this code works for me, now i am setting tintColor and backgroundColor to red to show you what's going on behind ...

+1


source







All Articles