Google Places iOS自动完成结果空间

时间:2019-03-03 12:20:26

标签: 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

1 个答案:

答案 0 :(得分:1)

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

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

resultsViewController.automaticallyAdjustsScrollViewInsets = false

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

resultsViewController.additionalSafeAreaInsets.top = searchBarContainer.frame.height

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