当方向改变时,以编程方式更改UISearchBar的框架

时间:2012-09-27 07:07:07

标签: ios ipad uisearchbar uiinterfaceorientation uisearchdisplaycontroller

我有一个搜索按钮,当用户点击它时,我会以编程方式显示UISearchBar。搜索栏的宽度为(200)不是全屏。我正在搜索MSWord并在searchbar'tableview中显示内容。但是当用户处于搜索中间并改变方向时,我应该如何控制搜索栏宽度,即self.searchDisplayController.searchBar.frame和self.searchDisplayController.searchResultsTableView.frame。

以下是用于创建搜索栏的代码:

-(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);
    }
}

我能够创建和功能正常。但唯一的问题是当用户更改orinentation时,它没有正确设置搜索栏的宽度。 我的申请需要在周五交付。请尽快回答我。

1 个答案:

答案 0 :(得分:3)

回答我的问题,关闭这个帖子以及帮助他人。

(1)在willRotateToInterfaceOrientation中它能够设置UISearchBar原点。宽度为768(纵向中iPad的全屏宽度),但原点似乎在这里设置。

(2)完成方向更改后,我们可以设置搜索栏宽度。使用小动画,它将平滑调整大小。

(3)我在前置和后置都调用resetFrame post orientation API,以避免searchBar的突然JURK类型。

以下是我用于我的应用程序的解决方案..

- (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);
    }
}