hightekjonathan
hightekjonathan

Reputation: 581

Use a UISearchBar and Search Controller on Xamarin.iOS

I currently have an app that contains a list of items, I now want to be able to search that list. I know Ill need to use a Search Bar and Controller, the only thing is, I can't find any documentation or examples for implementing this. I have a controller class setup for the search bar, but its a blank class. Where is a good starting point for this?

This question seems to be a good spot, but where does that go, and how do I port that to C# for xamarin?

Upvotes: 1

Views: 5674

Answers (3)

Alvin George
Alvin George

Reputation: 14294

Xamarin 5.10:

    var sampleSearchBar = new UISearchBar (new CoreGraphics.CGRect (20, 20, this.View.Frame.Width - 50, 40));
sampleSearchBar.SearchBarStyle = UISearchBarStyle.Prominent;

 sampleSearchBar.ShowsCancelButton = true;

    //Deleagte class source
  sampleSearchBar.Delegate = new SearchDelegate ();
 this.View.AddSubview (sampleSearchBar);

Add Delegate class to the ViewController.

class SearchDelegate : UISearchBarDelegate
        {
            public override void SearchButtonClicked (UISearchBar bar)
            {
                bar.ResignFirstResponder ();
            }

            public override void CancelButtonClicked (UISearchBar bar)
            {
                bar.ResignFirstResponder ();
            }

            public override bool ShouldBeginEditing (UISearchBar searchBar)
            {

                return true;
            }

            public override bool ShouldEndEditing (UISearchBar searchBar)
            {
                return true;
            }

            public override bool ShouldChangeTextInRange (UISearchBar searchBar, NSRange range, string text)
            {
                Console.WriteLine (searchBar.Text);
                return true;
            }
        }

Upvotes: 3

Lei Kan
Lei Kan

Reputation: 477

I create this demo for you with Storyboard and UISearchDisplayControlelr.

Look at this SearchDemo.

Upvotes: 1

Sport
Sport

Reputation: 8965

This is an iOS sample application that demonstrates how to use UISearchController. A search controller manages the presentation of a search bar (in concert with the results view controller’s content) Look at this and let me know if any issue after that. https://github.com/xamarin/monotouch-samples/tree/master/ios8/TableSearch

Upvotes: 1

Related Questions