GMSPlacePickerViewController UI垂直偏移问题咨询(push跳转场景)
解决GMSPlacePickerViewController垂直偏移与布局异常问题
我之前踩过GMSPlacePickerViewController用push跳转的布局坑,尤其是导航栏和Google原生控件的适配冲突,刚好碰到和你一样的问题——搜索列表首项截断、返回地图后顶部黑块,给你几个亲测有效的解决办法:
1. 手动适配滚动视图Insets
Google的PlacePicker内部默认是按present的布局逻辑设计的,push后导航栏会挤占顶部空间,导致滚动视图内容被截断。你可以在跳转前手动调整它的滚动视图内边距:
// 初始化你的GMSPlacePickerConfig和ViewController GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:nil]; GMSPlacePickerViewController *pickerVC = [[GMSPlacePickerViewController alloc] initWithConfig:config]; // 关闭自动调整滚动视图Insets,避免系统自动计算错误 pickerVC.automaticallyAdjustsScrollViewInsets = NO; // 计算导航栏+状态栏的总高度,手动设置内边距 CGFloat topOffset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height; // 遍历子视图找到内部的滚动视图(或直接通过tag定位,Google SDK内部滚动视图tag通常为1) UIScrollView *pickerScrollView = nil; for (UIView *subview in pickerVC.view.subviews) { if ([subview isKindOfClass:[UIScrollView class]]) { pickerScrollView = (UIScrollView *)subview; break; } } if (pickerScrollView) { pickerScrollView.contentInset = UIEdgeInsetsMake(topOffset, 0, 0, 0); pickerScrollView.scrollIndicatorInsets = pickerScrollView.contentInset; } [self.navigationController pushViewController:pickerVC animated:YES];
如果你的项目是iOS 11及以上,也可以用contentInsetAdjustmentBehavior替代自动调整:
pickerScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
2. 调整导航栏半透明属性
导航栏的半透明设置会影响子控制器的布局延伸,你可以尝试两种方案:
// 方案一:关闭导航栏半透明(如果你的UI风格允许) self.navigationController.navigationBar.translucent = NO; // 方案二:保持半透明,限制PlacePicker只延伸到底部,避免顶部被导航栏覆盖 pickerVC.edgesForExtendedLayout = UIRectEdgeBottom;
3. 修复返回地图后的顶部黑块
返回地图后出现黑块,通常是因为地图视图的布局没有重新计算适配当前视图边界。你可以在当前控制器的viewWillAppear方法里强制刷新布局:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 强制地图视图重新布局,适配当前视图的完整边界 self.mapView.frame = self.view.bounds; [self.mapView layoutIfNeeded]; }
额外提示
其实Google官方更推荐用presentViewController:的方式展示PlacePicker,这样它会自动适配全屏布局,避免导航栏的冲突。如果你的业务场景允许,改成present的方式能省不少适配的麻烦。
内容的提问来源于stack exchange,提问作者Hersh Bhargava
相关产品推荐
相关产品推荐

