ios - Google Places iOS 自动填充结果空间
问题描述
我尝试使用 Google 的自动完成 API,并使用了本教程: https ://developers.google.com/places/ios-sdk/autocomplete#add_a_search_bar_to_the_top_of_a_view
我的 viewDidLoad 方法包含以下内容:
[super viewDidLoad];
// google autocomplete
_resultsViewController = [[GMSAutocompleteResultsViewController alloc] init];
_resultsViewController.delegate = self;
[_resultsViewController setExtendedLayoutIncludesOpaqueBars:YES];
_searchController = [[UISearchController alloc]
initWithSearchResultsController:_resultsViewController];
_searchController.searchResultsUpdater = _resultsViewController;
[_searchController setDelegate:self];
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
self.definesPresentationContext = YES;
// Prevent the navigation bar from being hidden when searching.
_searchController.hidesNavigationBarDuringPresentation = NO;
self.navigationController.navigationBar.translucent = NO;
_searchController.hidesNavigationBarDuringPresentation = NO;
self.extendedLayoutIncludesOpaqueBars = YES;
self.edgesForExtendedLayout = UIRectEdgeTop;
[_searchBarContainerView addSubview:_searchController.searchBar];
[_searchController setActive:YES];
解决方案
我通过执行以下操作设法解决了这两个问题:
首先,为您的实例设置.automaticallyAdjustsScrollViewInsets
为:false
GMSAutocompleteResultsViewController()
resultsViewController.automaticallyAdjustsScrollViewInsets = false
如果您现在运行您的应用程序,您会看到多余的空间已经消失,但现在第一个结果在您的搜索栏下方。为了解决这个问题,我使用了该additionalSafeAreaInsets.top
属性(我建议将其设置为您searchBarContainerView
的高度,尽管您可以使用任何您想要的值):
resultsViewController.additionalSafeAreaInsets.top = searchBarContainer.frame.height
旁注:如果您支持 iOS 11 之前的设备,则必须将这些行包含在#available
检查中,因为additionalSafeAreaInsets
仅在 iOS 11+ 中可用。但是,在我自己的测试中,我发现 iOS 10 及更低版本一开始就没有这个问题。
推荐阅读
- java - 在Android中创建应用程序时如何只创建一次变量?
- c++ - 当服务器异常关闭时,boost.asio async_write 超时不起作用
- c# - 如何解决找不到方法:'System.Collections.Generic.IList`1
在 .Net Core 3.1 中 - laravel - 使用 Echo 事件广播时未定义的预加载关系
- python-3.x - Google Translate API 返回非 UTF8 字符
- r - 无法打开 lib 'FreeTDS':找不到文件并且 /etc/odbcinst.ini 丢失
- java - 在 HTML img 标签中使用 BaseMultiResolutionImage 支持的 IconImage
- javascript - Node.js Express 在服务器和客户端之间发送数据
- elasticsearch - 对 Elasticsearch 目标使用clevercloud drain
- ios - 在 SwiftUI 中使用 UITextInputPasswordRules