Search code examples
iphoneuialertviewsearchbar

How to create an alertview with search bar in iPhone?


I want to display an alert view with a search bar option on it. How can I create a search bar in a alert view? Any sample codes would be helpful.


Solution

  • I have created a search bar and and added the text box as its subview.

    UIAlertView * myAlertView = [[UIAlertView alloc] initWithTitle:@"         " 
                                                           message:@"         "
                                                          delegate:self 
                                                 cancelButtonTitle:nil 
                                                 otherButtonTitles:nil, nil];
    UISearchBar *myTextField = [[UISearchBar alloc] initWithFrame:CGRectMake(30.0, 35.0, 180.0, 35.0)];
    UIButton *searchbtn = [[UIButton alloc] initWithFrame:CGRectMake(230.0, 35.0, 40.0, 35.0)];
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [myAlertView setBackgroundColor:[UIColor grayColor]];
    [searchbtn setBackgroundColor:[UIColor grayColor]];
    [myAlertView addSubview:myTextField];
    [myAlertView addSubview:searchbtn];
    

    If there is any other better way please let me know.