xcode 激活搜索栏不会隐藏导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19348819/
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
Activating search bar doesn't hide navigation bar
提问by Ian
I have an iOS 7 application in XCode 5.0 that exhibits some strange behavior when tapping the search bar (UISearchBar
).
我在 XCode 5.0 中有一个 iOS 7 应用程序,它在点击搜索栏 ( UISearchBar
)时表现出一些奇怪的行为。
My application has a Navigation Controller, and a Tab Bar Controller. Here is an example of what my Main.Storyboard looks like:
我的应用程序有一个导航控制器和一个标签栏控制器。这是我的 Main.Storyboard 的示例:
[Navigation Controller] -> [Tab Bar Controller] -> [Tab Item #1]
|
-------------> [Tab Item #2]
Each [] is a view controller
When I launch my application, I see the Tab Item 1
with the UISearchBar
as shown in the screenshot below:
当我启动我的申请,我看到了Tab Item 1
与UISearchBar
如下面的截图:
When I tap the UISearchBar
, the search bar slides up to the top of the screen, but the Navigation Bar does not hide, and the view does not "slide up". This causes the app to look like this:
当我点击 时UISearchBar
,搜索栏会向上滑动到屏幕顶部,但导航栏不会隐藏,视图也不会“向上滑动”。这会导致应用程序看起来像这样:
When I delete the Tab Bar Controller
from the storyboard and connect the Navigation Controller
directly to Tab Item #1
the Navigation Bar hides as expected.
当我Tab Bar Controller
从故事板中删除并Navigation Controller
直接连接到Tab Item #1
导航栏时,会按预期隐藏。
How can I make the Navigation Bar hide when tapping the Search Bar? For an example of the functionality I am looking to reproduce, click the search bar under the "Contacts" tab of the default iOS7 "Phone" application.
如何在点击搜索栏时隐藏导航栏?有关我希望重现的功能的示例,请单击默认 iOS7“电话”应用程序的“联系人”选项卡下的搜索栏。
回答by Rubaiyat Jahan Mumu
For swiftdevelopers:
对于swift开发人员:
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
navigationController?.setNavigationBarHidden(true, animated: true)
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
navigationController?.setNavigationBarHidden(false, animated: true)
}
This will hide the navigation bar while search bar is active and show it again when search bar is inactive.
这将在搜索栏处于活动状态时隐藏导航栏,并在搜索栏处于非活动状态时再次显示。
回答by Olle Lind
You can use the UISearchBar delegate methods to decide when to move the navigationbar out of the screen.
您可以使用 UISearchBar 委托方法来决定何时将导航栏移出屏幕。
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[UIView animateWithDuration:0.2 animations:^{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
double yDiff = self.navigationController.navigationBar.frame.origin.y - self.navigationController.navigationBar.frame.size.height - statusBarFrame.size.height;
self.navigationController.navigationBar.frame = CGRectMake(0, yDiff, 320, self.navigationController.navigationBar.frame.size.height);
}];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[UIView animateWithDuration:0.2 animations:^{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
double yDiff = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height;
self.navigationController.navigationBar.frame = CGRectMake(0, yDiff, 320, self.navigationController.navigationBar.frame.size.height);
}];
}
回答by Lkoy
You can set the top bar in the navigation controller for the list, to none and then add this to your tabBarController code:
您可以将列表导航控制器中的顶部栏设置为无,然后将其添加到您的 tabBarController 代码中:
self.navigationController.navigationBar.translucent= NO;
In the viewDidLoad method
在 viewDidLoad 方法中
回答by Kemal Can Kaynak
You can do it with UISearchDisplayController method;
您可以使用 UISearchDisplayController 方法来实现;
-(void)searchDisplayControllerWillBeginSearch:(mySearchDisplayController *)controller
{
self.searchResultsDataSource = self;
self.searchResultsTableView.delegate = self;
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
[UIView animateWithDuration:0.01 animations:^{
for (UIView *subview in self.searchBar.subviews)
subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height);
}];
}
}
-(void)searchDisplayControllerWillEndSearch:(mySearchDisplayController *)controller
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
[UIView animateWithDuration:0.01 animations:^{
for (UIView *subview in self.searchBar.subviews)
subview.transform = CGAffineTransformIdentity;
}];
}
}
Don't forget create a new class as a type of UISearchDisplayController and implement that code in it.
不要忘记创建一个新类作为 UISearchDisplayController 的类型并在其中实现该代码。
回答by Chowdhury Md Rajib Sarwar
The following line will hide the navigation bar animatedly on activating search bar.
以下行将在激活搜索栏时动画隐藏导航栏。
self.searchController.hidesNavigationBarDuringPresentation = true
回答by For Guru
I solved the problem by adding constraint on Search Bar to Top Layout Guide and set its value to 0 and added a vertical spacing constraint to Search Bar and setting its value to 0;
我通过将搜索栏上的约束添加到顶部布局指南并将其值设置为 0 并为搜索栏添加了垂直间距约束并将其值设置为 0 解决了该问题;
回答by Vivek Seth
I realize that this is probably too late to help you, but I ran into the same issue today!
我意识到这可能为时已晚,无法帮助您,但我今天遇到了同样的问题!
I solved it by fixing the constraints of the search bar. Make sure the search bar has a constraint of 0px for its immediate top neighbor (the nav bar). Also make sure the tableview below the search bar has a constraint of 0px for its immediate top neighbor (the search bar).
我通过修复搜索栏的约束来解决它。确保搜索栏对其直接顶部邻居(导航栏)的约束为 0px。还要确保搜索栏下方的 tableview 对其直接顶部邻居(搜索栏)的约束为 0px。
Not sure of the exact problem you were facing, but that fixed it for me.
不确定您面临的确切问题,但这为我解决了问题。