Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Custom searchbar hack.

$
0
0

I have seen several questions about implementing a custom search bar, which may have been on the product roadmap according to Paul Dowsett. I haven't seen any such documentation, so I apologize if I missed it.

Anyway, here is my hack to add a custom searchbar to a TableView (in the form of a TextField).

// Create a view containing the text field. //
    var customSearchBar = Ti.UI.createView({
        height:44,
        top:0,
        width:320,
        backgroundColor:'green'
    });
 
    var customSearchField = Ti.UI.createTextField({
        width:220,
        hintText:'search',
        autocorrect:false,
        height:28,
        top:8,
        textAlign:'center',
        clearOnEdit:true,
        borderStyle:Ti.UI.INPUT_BORDERSTYLE_ROUNDED
    });
 
    customSearchBar.add(customSearchField);
 
// Create a transparent SearchBar. //
    var search = Ti.UI.createSearchBar({
        top:0,
        opacity:0,
        autocorrect:false
    });
 
// Create your TableView. Do not hide the transparent SearchBar. //
    var tableView = Ti.UI.createTableView({
        separatorColor:'green',
        backgroundColor:'white',
        filterAttribute:'title',
        filterCaseInsensitive:true,
        searchHidden:false,
        search:search,
        top:0
    });
 
// Add the TableView to your window first. //
    win.add(tableView);
 
// Add the custom searchbar on top of your table view, covering the transparent SearchBar. //
    win.add(customSearchBar);
 
// Provide the input value for the TableView to filter (per KitchenSink). //    
    search.addEventListener('change', function(e) {
        e.value;
    });
 
// Cycle through each element and apply the new value. //
    customSearchField.addEventListener('change',function(e) {
        search.focus();
        search.value = e.value;
        customSearchField.focus();
        customSearchField.value = e.value;
    });
Tested with custom rows + child views on iPhone 4+.

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>