我尝试使用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];
但是它显示的结果类似于图像。如何删除结果和搜索栏之间的空间?
最佳答案
通过执行以下操作,我设法解决了这两个问题:
首先,将.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/