1

我有一個搜索按鈕,當用戶單擊它時,我以編程方式顯示UISearchBar。搜索欄的寬度(200)不是全屏。我像MSWord一樣搜索並在搜索欄'tableview中顯示內容。但是,當用戶處於搜索中間並改變方向時,我應該如何控制搜索欄寬度,即self.searchDisplayController.searchBar.frame和self.searchDisplayController.searchResultsTableView.frame。當方向發生變化時,以編程方式更改UISearchBar的框架

下面是用於創建搜索欄的代碼段:

-(IBAction)searchBar:(id)sender 
{ 
    if(!searching) 
    { 
     searching = YES; 
     sBar.frame = CGRectMake(500, 50,250, 44); 
     [self.view addSubview:sBar]; 
     [searchController setActive:YES animated:YES]; 
     [sBar becomeFirstResponder]; 

     if((currentOrientation == UIInterfaceOrientationLandscapeLeft) || 
      (currentOrientation == UIInterfaceOrientationLandscapeRight)) 
     { 
      self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44); 
     } 
     else 
     { 
      self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44); 
     } 
    } 
} 


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ 
    [self filterContentForSearchText:searchString scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 

    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption 
{ 
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 

    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{ 
    /* 
    Bob: Because the searchResultsTableView will be released and allocated automatically, so each time we start to begin search, we set its delegate here. 
    */ 
    [self.searchDisplayController.searchResultsTableView setDelegate:self]; 
    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:204/255.0 alpha:1.0]]; 
} 

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{ 
    /* 
    Hide the search bar 
    */ 

    float animateTime = 0.3; 
    CGRect viewFrame = sBar.frame; 
    viewFrame.origin.y -= (viewFrame.size.height); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:animateTime]; 

    sBar.frame = viewFrame; 

    [UIView commitAnimations]; 
    [self performSelector:@selector(animationDone) withObject:nil afterDelay:0.3]; 

} 

-(void)animationDone 
{ 
    [sBar removeFromSuperview]; 
    searching = NO; 
} 


-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView 
{ 
    if((currentOrientation == UIInterfaceOrientationLandscapeLeft) || 
     (currentOrientation == UIInterfaceOrientationLandscapeRight)) 
    { 
     tableView.frame = CGRectMake(755, 100, 250, 400); 
    } 
    else 
    { 
     tableView.frame = CGRectMake(500, 100, 250, 400); 
    } 
} 



- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration 
{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    currentOrientation = toInterfaceOrientation; 



    if((currentOrientation == UIInterfaceOrientationLandscapeLeft) || 
     (currentOrientation == UIInterfaceOrientationLandscapeRight)) 
    { 

     //Search 
     btnSearch.frame = CGRectMake(920, 5, 40, 40); 

     self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44); 
     self.searchDisplayController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400); 
    } 
    else 
    { 

     //Search 
     btnSearch.frame = CGRectMake(666, 5, 40, 40); 

     self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44); 
     self.searchDisplayController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400); 
    } 
} 

我能夠創建和工作正常功能。但唯一的問題是,當用戶更改標誌不正確設置搜索欄的寬度。 我的申請需要在星期五遞交。請儘快回覆我。

回答

3

回答我的問題以關閉此主題以及幫助他人。

(1)在willRotateToInterfaceOrientation中它能夠設置UISearchBar的原點。寬度是768(縱向iPad的全屏寬度),但起源似乎在這裏設置。 (2)方向更改完成後,我們可以設置搜索欄寬度。隨着小動畫,它將平穩調整大小。

(3)我在前&後定位API調用resetFrame,以避免一些突發的JURK類型的searchBar。

下面是我用我的應用程序解決方案..

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration 
{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    currentOrientation = toInterfaceOrientation; 



    [self resetSearchResourcesFrames]; // Set the origin of UISearchBar 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
    [UIView beginAnimations:@"" context:nil]; //Animation for smooth resizing of search tableview only 
    [UIView setAnimationDuration:0.2]; 
    [self resetSearchResourcesFrames]; // Width rotation only possible after rotation 
    [UIView commitAnimations]; 
} 

-(void)resetSearchResourcesFrames 
{ 
    if(([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeLeft) || 
     ([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeRight)) 
    { 
     //Search 
     btnSearch.frame = CGRectMake(920, 5, 40, 40); 

     self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44); 
     self.searchController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400); 
    } 
    else 
    { 
     //Search 
     btnSearch.frame = CGRectMake(666, 5, 40, 40); 

     self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44); 
     self.searchController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400); 
    } 
} 
相關問題