Detailed Explanation and Example of UISearchBar Developed by IOS

  • 2021-08-12 03:49:52
  • OfStack

IOS UISearchBar Explanation

iPhone development of UISearchBar learning is the content of this article to learn, mainly introduced the use of UISearchBar, not to say, let's first look at the details. Some questions about UISearchBar.

1. Modify the background color of UISearchBar

UISearchBar is composed of two subView, one is UISearchBarBackGround and the other is UITextField. Attributes that have no direct manipulation background in IB are required. The method is to remove UISearchBarBackGround directly


seachBar=[[UISearchBar alloc] init]; 
seachBar.backgroundColor=[UIColor clearColor]; 
for (UIView *subview in seachBar.subviews)  
{  
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    {  
[subview removeFromSuperview];  
break; 
}  
} 

The second solution:


[[searchbar.subviews objectAtIndex:0]removeFromSuperview]; 

2.


UISearchBar* m_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 44, 320, 41)]; 
m_searchBar.delegate = self; 
m_searchBar.barStyle = UIBarStyleBlackTranslucent; 
m_searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
m_searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; 
m_searchBar.placeholder = _(@"Search"); 
m_searchBar.keyboardType = UIKeyboardTypeDefault; 
// For UISearchBar Add Background Picture  
UIView *segment = [m_searchBar.subviews objectAtIndex:0]; 
UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Images/search_bar_bg.png"]]; 
[segment addSubview: bgImage]; 
//<--- Background picture  
[self.view addSubview:m_searchBar]; 
[m_searchBar release]; 

3: Cancel the keyboard of UISearchBar call


[searchBar resignFirstResponder]; 

Two ways to add UISearchBar:

Code


UISearchBar *mySearchBar = [[UISearchBar alloc] 
initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];     
 mySearchBar.delegate = self;     
 mySearchBar.showsCancelButton = NO;     
 mySearchBar.barStyle=UIBarStyleDefault;     
 mySearchBar.placeholder=@"Enter Name or Categary";      
mySearchBar.keyboardType=UIKeyboardTypeNamePhonePad;      
[self.view addSubview:mySearchBar];     
 [mySearchBar release];  

Add on tableview:

Code


 //add Table 
    UITableView *myBeaconsTableView = [[UITableView alloc]  
                      initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-40)  
                       style:UITableViewStylePlain]; 
    myBeaconsTableView.backgroundColor = [UIColor whiteColor]; 
    myBeaconsTableView.delegate=self; 
    myBeaconsTableView.dataSource=self; 
    [myBeaconsTableView setRowHeight:40]; 
    // Add searchbar  
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)]; 
    searchBar.placeholder=@"Enter Name"; 
    searchBar.delegate = self; 
    myBeaconsTableView.tableHeaderView = searchBar; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    [searchBar release]; 
    [self.view addSubview:myBeaconsTableView]; 
    [myBeaconsTableView release]; 

Summary: iPhone development of UISearchBar learning content introduced, I hope this article to help you


Related articles: