ios - Google Places iOS自动完成结果空间

标签 ios objective-c google-places-api

我尝试使用Google的Autocomplete 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];

但是它显示的结果类似于图像。如何删除结果和搜索栏之间的空间?
enter image description here

最佳答案

通过执行以下操作,我设法解决了这两个问题:

首先,将.automaticallyAdjustsScrollViewInsets实例的false设置为GMSAutocompleteResultsViewController():

resultsViewController.automaticallyAdjustsScrollViewInsets = false

如果您现在运行应用程序,将会看到多余的空间已消失,但是现在第一个结果在搜索栏的下方。为了解决这个问题,我使用了additionalSafeAreaInsets.top属性(尽管可以使用所需的任何值,但我建议将其设置为searchBarContainerView的高度):
resultsViewController.additionalSafeAreaInsets.top = searchBarContainer.frame.height

旁注:如果支持iOS 11之前的设备,则必须将这些行包装在#available检查中,因为additionalSafeAreaInsets仅在iOS 11+中可用。但是,在我自己的测试中,我发现iOS 10及更低版本没有这个问题。

关于ios - Google Places iOS自动完成结果空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54968730/

相关文章:

ios - 在 iOS 设备上本地保存解析对象

javascript - AngularJS 范围变量在另一个函数内更改时不会更新

ios - 按地址和地名自动完成搜索栏 - Swift 4

android - 从 Google Places 结果中获取经度和纬度

iphone - UITableviewCell 意外更改

ios - 如何使用文本字段和按钮将文本添加到每个单元格中?

iOS UILabel 自动收缩所以单词不会截断为两行

iphone - 如何使注释副标题从标题菜单中消失?

ios - 如何使用swift2以编程方式获取Apple Watch序列号?

ios - 循环遍历嵌套的 NSMutableDictionary 并过滤一些项目