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
- seachBar=[[UISearchBar alloc] init];
- seachBar.backgroundColor=[UIColor clearColor];
- for (UIView *subview in seachBar.subviews)
- {
- if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
- {
- [subview removeFromSuperview];
- break;
- }
- }
Solution 2:
- [[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;
- // Add a background image for UISearchBar
- UIView * segment = [m_searchBar.subviews objectAtIndex: 0];
- UIImageView * bgImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "Images/search_bar_bg.png"];
- [Segment addSubview: bgImage];
- // <--- Background image
- [Self. view addSubview: m_searchBar];
- [M_searchBar release];
3: cancel the keyboard called by UISearchBar
- [searchBar resignFirstResponder];
You can add UISearchBar in either of the following ways:
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:
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 DevelopmentOfUISearchBarI hope this article will be helpful to you.