我尝试使用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];
答案 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及更低版本没有这个问题。