iOS 7 UISearchDisplayController 搜索栏在搜索时与状态栏重叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18925900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 7 UISearchDisplayController search bar overlaps status bar while searching
提问by desmondhume
I'm updating my app for iOS 7, and I'm in the process of adjusting all my views to account for the new transparent status bar (my app will still use opaque navigation bars).
我正在为 iOS 7 更新我的应用程序,我正在调整我的所有视图以适应新的透明状态栏(我的应用程序仍将使用不透明的导航栏)。
It was relatively easy to adjust for the status bar in every view, except one major problem I'm having with a UISearchBar connected to a UISearchDisplayController in one of my view controllers.
在每个视图中调整状态栏相对容易,除了我在一个视图控制器中将 UISearchBar 连接到 UISearchDisplayController 时遇到的一个主要问题。
The search bar seems to display normally, as shown below:
搜索栏好像显示正常,如下图:
The problem is, as soon as I begin searching, the navigation bar disappears (as it should), but everything else also moves up to overlap the status bar:
问题是,一旦我开始搜索,导航栏就会消失(应该如此),但其他所有内容也会向上移动以与状态栏重叠:
This doesn't appear to be working as intended, since the darkening of the screen happens 20 pixels below the search bar, where the search bar should end.
这似乎没有按预期工作,因为屏幕变暗发生在搜索栏下方 20 像素处,搜索栏应在此处结束。
Is there a built in solution for this in iOS 7? I'd rather not have to manually adjust the frame for every view each time the user begins and ends searching.
iOS 7 中是否有针对此的内置解决方案?我宁愿不必在用户每次开始和结束搜索时手动调整每个视图的框架。
Thanks!
谢谢!
采纳答案by desmondhume
Thank you hodade for leading me on the right track! Your solution worked, except it only moved the search bar's frame, leaving my other subviews in the wrong spot. The only thing I changed was to move all the subviews in my view, as well as animate it.
感谢 hodade 带领我走上正轨!您的解决方案有效,只是它只移动了搜索栏的框架,而将我的其他子视图留在了错误的位置。我唯一改变的是在我的视图中移动所有子视图,并为其设置动画。
Thanks!
谢谢!
-(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;
}];
}
}
回答by Deddiekoel
Putting the following line in the viewDidLoad fixed it for me:
将以下行放在 viewDidLoad 中为我修复了它:
self.edgesForExtendedLayout = UIRectEdgeNone;
回答by tachiba
You're may using no translucent navigation bar? If so, this will solve it.
你可能没有使用半透明的导航栏?如果是这样,这将解决它。
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
self.navigationController.navigationBar.translucent = YES;
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
self.navigationController.navigationBar.translucent = NO;
}
回答by MaxEcho
Just place following code in -(void) ViewDidLoad. It will work for iOS 7 and later version
只需将以下代码放在-(void) ViewDidLoad 中。它适用于 iOS 7 及更高版本
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
UPDATE:
更新:
if(SYSTEM_VERSION_GREATER_THAN(@"6.1")) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
回答by Morkrom
This seems to describe the problem I was having; hopefully it will help someone in my former positon.
这似乎描述了我遇到的问题;希望它会帮助我以前的职位的人。
Subclass your SearchDisplayController that's been added to your UIViewController/UITablewViewController,
Add something like this to its implementation:
- (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]; }
继承已添加到 UIViewController/UITablewViewController 的 SearchDisplayController,
将这样的内容添加到其实现中:
- (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]; }
回答by Sabareesh
I did below code to solve the problem.
我做了下面的代码来解决这个问题。
- (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;
}
}
回答by EVA
I think maybe add this to viewDidLoad will help:
我想也许将它添加到 viewDidLoad 会有所帮助:
if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
回答by Emperio Superiority
just add
只需添加
self.definesPresentationContext = YES;
you can read more from here: UISearchController and definesPresentationContext
您可以从这里阅读更多信息:UISearchController 和definePresentationContext
and from Apple documentation: UISearchController documentation
以及来自 Apple 文档:UISearchController 文档
Note:UISearchDispalyController is deprecated in iOS7, use UISearchController instead in iOS8, method above use UISearchController
注意:UISearchDispalyController 在 iOS7 中被弃用,在 iOS8 中使用 UISearchController 代替,上面的方法使用 UISearchController
回答by Aurelien Porte
In my case, the views below the search bar were at their right place, only the search bar was overlapping the status bar. In this case, this peace of code works fine:
就我而言,搜索栏下方的视图位于正确的位置,只有搜索栏与状态栏重叠。在这种情况下,这种代码的和平工作正常:
- (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;
}
}
Hope it will be useful to others
希望对其他人有用
回答by Andrew Schreiber
The above answers only work if you have your navigation bar unhidden. If you have your navigation bar hiddentry this:
以上答案仅在您的导航栏未隐藏时才有效。如果您隐藏了导航栏,请尝试以下操作:
-(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)];
}
}
Based on this post: UISearchBar overlaps status bar in iOS
基于这篇文章:UISearchBar 与 iOS 中的状态栏重叠