UISearchBar learning for iPhone Development

Source: Internet
Author: User

IPhone DevelopmentOfUISearchBarLearning is the content to be learned in this article.UISearchBarLet's take a look at the details. AboutUISearchBar.

1. ModifyUISearchBarBackground Color

UISearchBar consists of two subviews, one is UISearchBarBackGround, and the other is UITextField. If IB does not have a direct operation background attribute. You can directly move UISearchBarBackGround

 
 
  1. seachBar=[[UISearchBar alloc] init];  
  2. seachBar.backgroundColor=[UIColor clearColor];  
  3. for (UIView *subview in seachBar.subviews)   
  4. {    
  5. if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  
  6.        {    
  7. [subview removeFromSuperview];    
  8. break;  
  9. }   

Solution 2:

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

2,

 
 
  1. UISearchBar * m_searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake (0, 44,320, 41)];
  2. M_searchBar.delegate = self;
  3. M_searchBar.barStyle = UIBarStyleBlackTranslucent;
  4. M_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
  5. M_searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
  6. M_searchBar.placeholder = _ (@ "Search ");
  7. M_searchBar.keyboardType = UIKeyboardTypeDefault;
  8. // Add a background image for UISearchBar
  9. UIView * segment = [m_searchBar.subviews objectAtIndex: 0];
  10. UIImageView * bgImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "Images/search_bar_bg.png"];
  11. [Segment addSubview: bgImage];
  12. // <--- Background image
  13. [Self. view addSubview: m_searchBar];
  14. [M_searchBar release];

3: cancel the keyboard called by UISearchBar

 
 
  1. [searchBar resignFirstResponder];  

You can add UISearchBar in either of the following ways:

Code

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

Add:

Code

 
 
  1. //add Table  
  2.         UITableView *myBeaconsTableView = [[UITableView alloc]   
  3.                                            initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-40)    
  4.   style:UITableViewStylePlain];  
  5.         myBeaconsTableView.backgroundColor = [UIColor whiteColor];  
  6.         myBeaconsTableView.delegate=self;  
  7.         myBeaconsTableView.dataSource=self;  
  8.         [myBeaconsTableView setRowHeight:40];  
  9.         // Add searchbar   
  10.         searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)];  
  11.         searchBar.placeholder=@"Enter Name";  
  12.         searchBar.delegate = self;  
  13.         myBeaconsTableView.tableHeaderView = searchBar;  
  14.         searchBar.autocorrectionType = UITextAutocorrectionTypeNo;  
  15.         searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;  
  16.         [searchBar release];  
  17.         [self.view addSubview:myBeaconsTableView];  
  18.         [myBeaconsTableView release];  

Summary:IPhone DevelopmentOfUISearchBarI hope this article will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.