iOS 7 UISearchDisplayController搜索栏在搜索时重叠状态栏

时间:2013-09-20 21:08:08

标签: ios uisearchbar ios7 statusbar uisearchdisplaycontroller

我正在为iOS 7更新我的应用程序,并且我正在调整所有视图以考虑新的透明状态栏(我的应用程序仍将使用不透明的导航栏)。

在每个视图中调整状态栏相对容易,除了我在一个视图控制器中连接到UISearchDisplayController的UISearchBar时遇到的一个主要问题。

搜索栏似乎正常显示,如下所示:

Search Bar http://imageshack.us/a/img163/9128/06vx.png

问题是,一旦我开始搜索,导航栏就会消失(应该如此),但其他一切也会向上移动以重叠状态栏:

Broken Search Bar http://imageshack.us/a/img11/8237/corh.png

这似乎没有按预期工作,因为屏幕变暗发生在搜索栏下方20个像素处,搜索栏应该在此处结束。

在iOS 7中是否有针对此的内置解决方案?每次用户开始和结束搜索时,我都不必为每个视图手动调整帧。

谢谢!

12 个答案:

答案 0 :(得分:88)

将以下行放在viewDidLoad中为我修复:

self.edgesForExtendedLayout = UIRectEdgeNone;

答案 1 :(得分:21)

感谢hoda​​de带领我走上正轨!您的解决方案有效,除了它只移动了搜索栏的框架,使我的其他子视图处于错误的位置。我改变的唯一方法是在我的视图中移动所有子视图,并为其设置动画。

谢谢!

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        [UIView animateWithDuration:0.25 animations:^{
            for (UIView *subview in self.view.subviews)
                subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height);
        }];
    }
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [UIView animateWithDuration:0.25 animations:^{
            for (UIView *subview in self.view.subviews)
                subview.transform = CGAffineTransformIdentity;
        }];
    }
}

答案 2 :(得分:13)

您可能没有使用半透明导航栏吗? 如果是这样,这将解决它。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = YES;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = NO;
}

答案 3 :(得分:7)

只需在 - (void)ViewDidLoad 中放置以下代码即可。它适用于iOS 7及更高版本

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

<强>更新

if(SYSTEM_VERSION_GREATER_THAN(@"6.1")) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

答案 4 :(得分:6)

这似乎描述了我遇到的问题;希望它会帮助我以前的位置。

  1. 将已添加到UIViewController / UITablewViewController的SearchDisplayController子类化,

  2. 在其实现中添加类似的内容:

     - (void)setActive:(BOOL)visible animated:(BOOL)animated
    {
        [super setActive:visible animated:animated];
    
        [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
    
        CGRect frame = self.searchResultsTableView.frame;
        frame.origin.y = CGRectGetHeight(self.searchContentsController.navigationController.navigationBar.frame);
    
        frame.size.height = CGRectGetHeight(frame) - CGRectGetMinY(frame);
    
        self.searchResultsTableView.frame = frame;
    
        frame = self.searchBar.frame;
        self.searchBar.frame = frame;
    
        [self.searchContentsController.view insertSubview:self.searchBar aboveSubview:self.searchResultsTableView];
    
    }
    

答案 5 :(得分:6)

我在下面的代码中解决了这个问题。

    - (void) viewDidLayoutSubviews
{
         if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
        {
            CGRect viewBounds = self.view.bounds;
            CGFloat topBarOffset = self.topLayoutGuide.length;
            viewBounds.origin.y = topBarOffset * -1;
            self.view.bounds = viewBounds;
        }
}

答案 6 :(得分:5)

我想可能会将此添加到viewDidLoad中会有所帮助:

if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
    self.edgesForExtendedLayout = UIRectEdgeNone;

}

答案 7 :(得分:3)

只需添加

self.definesPresentationContext = YES;

您可以在此处阅读更多内容:UISearchController and definesPresentationContext

来自Apple文档:UISearchController documentation

注意: UISearchDispalyController在iOS7中已弃用,在iOS8中使用UISearchController,上面的方法使用UISearchController

答案 8 :(得分:2)

在我的情况下,搜索栏下方的视图位于正确的位置,只有搜索栏与状态栏重叠。在这种情况下,这种代码的和平工作正常:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {        
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = self.searchBar.frame;
        frame.origin.y += statusBarFrame.size.height;
        self.searchBar.frame = frame;
    }
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = self.searchBar.frame;
        frame.origin.y -= statusBarFrame.size.height;
        self.searchBar.frame = frame;
    }
}

希望它对其他人有用

答案 9 :(得分:0)

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = controller.searchBar.frame;
        frame.origin.y += statusBarFrame.size.height;
        controller.searchBar.frame = frame;
    }
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = controller.searchBar.frame;
        frame.origin.y -= statusBarFrame.size.height;
        controller.searchBar.frame = frame;
    }
}

答案 10 :(得分:0)

以上答案仅适用于导航栏取消隐藏的情况。如果隐藏了导航栏,请尝试以下操作:

-(void)viewDidAppear:(BOOL)animated{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
    [self.tableView setFrame:CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y+statusBarFrame.size.height, self.tableView.frame.size.width, self.tableView.frame.size.height)];

    }
}

根据这篇文章:UISearchBar overlaps status bar in iOS

答案 11 :(得分:0)

将已添加到SearchDisplayController的{​​{1}}小类,并将其添加到其实施中 -

UIViewController/UITablewViewController