iOS的7 UISearchDisplayController搜索栏重叠状态栏,而搜索iOS的7 U

2019-05-13 02:18发布

我更新我的应用程序适用于iOS 7,我在我调整所有的意见,以考虑新的透明状态栏(我的应用程序仍然会使用不透明的导航栏)的过程。

这是比较容易调整为每个视图状态栏上,除了我和我的视图控制器的一个连接到UISearchDisplayController一个UISearchBar有一个主要的问题。

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

搜索栏http://imageshack.us/a/img163/9128/06vx.png

问题是,当我开始搜索,导航栏消失(因为它应该),但一切也向上移动重叠状态栏:

残破的搜索栏http://imageshack.us/a/img11/8237/corh.png

这似乎并没有被工作如预期,因为屏幕变暗发生的搜索栏,在搜索栏应该结束低于20个像素。

是否有一个内置的解决方案,这在iOS的7? 我宁愿不必须手动调整所述框架为每一个用户每次开始和结束搜索时间查看。

谢谢!

Answer 1:

把下面一行在viewDidLoad中固定对我来说:

self.edgesForExtendedLayout = UIRectEdgeNone;


Answer 2:

感谢您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;
        }];
    }
}


Answer 3:

你可以不使用半透明的导航栏? 如果是这样,这将解决这个问题。

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

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


Answer 4:

只需将下面的代码- (无效)viewDidLoad中 。 它会为iOS 7和更高版本工作

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

更新:

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


Answer 5:

我做了下面的代码来解决这个问题。

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


Answer 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]; } 


Answer 7:

我想也许添加这viewDidLoad中会有所帮助:

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

}


Answer 8:

只需添加

self.definesPresentationContext = YES;

你可以阅读更多来自这里: UISearchController和definesPresentationContext

而从苹果文档: UISearchController文档

注意:UISearchDispalyController在iOS7弃用,使用UISearchController代替iOS8上,上述方法使用UISearchController



Answer 9:

就我而言,在搜索栏下方的观点是他们的正确的地方,只有在搜索栏被重叠的状态栏。 在这种情况下,这个代码的和平正常工作:

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

希望这将是有用的人



Answer 10:

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


Answer 11:

如果你有你的导航栏取消隐藏以上答案只是工作。 如果你有你的导航栏隐藏试试这个:

-(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状态栏



Answer 12:

继承您的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];
    }
}


文章来源: iOS 7 UISearchDisplayController search bar overlaps status bar while searching