2014-01-09 53 views
2

我在我的应用程序中有UISearchBar。我将它设置为表头视图。当文本开始编辑时,我想将搜索栏设置为我的viewController的子视图。搜索栏正确添加到viewController的视图,但它的框架不正确。这是我的代码:UIsearchBar。无法更改框架

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    UITableView *tableView = ((UITableView *)[((SectionViewController *)sharedInstance.currentViewController).view viewWithTag:1111]); 
    [tableView reloadData]; 

    if([searchBar.text isEqualToString:@"Filter..."]) 
    { 
     searchBar.text = @""; 
     for(UIView *subView in searchBar.subviews){ 
      if([subView isKindOfClass:UITextField.class]){ 
       [(UITextField*)subView setTextColor:[UIColor blackColor]]; 
      } 
     } 
    } 
    [((SectionViewController *)sharedInstance.currentViewController).view addSubview:searchBar]; 
    [searchBar setFrame:CGRectMake(0, 20, 320, 38)]; 

    [((SectionViewController *)sharedInstance.currentViewController).navigationController setNavigationBarHidden:YES animated:YES]; 
    [searchBar setShowsCancelButton:YES animated:YES]; 
} 

我想设置帧(0,20,320,38),但我得到(0,0,320,38)。而且,searchBar的初始帧是(0,0,320,38)。请告诉我为什么我得到这个?

+0

如果你不打算使用“UISearchDisplayController”,尝试通过使用UITextField,你可以使它看起来像一个UISerchBar,它会更可定制 – Camo

回答

2

UISearchBar很奇怪。你不能自由设定其框架。这是由于内部实施。

0

我添加的UISearchBar这样对我的ViewController这是从衍生的UITableViewController:

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,self.tableView.frame.size.width,44)]; 
searchBar.placeholder = @"Search for Groups"; 
searchBar.delegate = self; 
self.searchController = [[UISearchDisplayController alloc] 
     initWithSearchBar:searchBar 
     contentsController:self]; 
self.searchController.delegate = self; 
self.searchController.searchResultsDataSource = self; 
self.searchController.searchResultsDelegate = self; 
self.tableView.tableHeaderView = self.searchController.searchBar; 
0

视图不能在两个地方一次。您不能连贯地将相同的搜索栏作为子视图添加到视图控制器的视图中,同时它也是表视图的标题。为什么不制作不同搜索栏?