66

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

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

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

搜索栏

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

损坏的搜索栏

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

iOS 7 中是否有内置解决方案?我宁愿不必在每次用户开始和结束搜索时手动调整每个视图的框架。

谢谢!

4

12 回答 12

88

将以下行放入 viewDidLoad 为我修复它:

self.edgesForExtendedLayout = UIRectEdgeNone;
于 2013-09-25T20:43:32.287 回答
21

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

谢谢!

-(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;
        }];
    }
}
于 2013-09-24T18:11:50.780 回答
13

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

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

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = NO;
}
于 2014-02-02T13:16:17.050 回答
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;
}
于 2014-08-25T09:36:15.060 回答
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;
        }
}
于 2013-10-22T11:16:44.077 回答
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];
    
    }
    
于 2013-10-20T02:41:12.140 回答
5

我认为也许将其添加到 viewDidLoad 会有所帮助:

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

}
于 2013-11-19T03:21:47.010 回答
3

只需添加

self.definesPresentationContext = YES;

您可以从这里阅读更多内容:UISearchController 和定义PresentationContext

并来自 Apple 文档:UISearchController 文档

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

于 2014-12-21T16:31:56.123 回答
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;
    }
}

希望对其他人有用

于 2013-10-26T17:16:03.907 回答
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 与 iOS 中的状态栏重叠

于 2013-12-07T00:59:55.330 回答
0

SearchDisplayController将已添加到您的您的子类化UIViewController/UITablewViewController并将其添加到其实现中-

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
    if(self.active == visible) return;
    [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
    [super setActive:visible animated:animated];
    [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
    if (visible) {
        [self.searchBar becomeFirstResponder];
    } else {
        [self.searchBar resignFirstResponder];
    }
}
于 2015-03-13T10:56:05.513 回答
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;
    }
}
于 2013-09-23T13:59:15.823 回答