我想知道是否可以将我的位置按钮图像从 GMSMapView 更改为自定义图像。
问问题
3026 次
3 回答
4
1)知道帧位置按钮:
for (UIView *object in self.mapView.subviews) {
if([[[object class] description] isEqualToString:@"GMSUISettingsView"] )
{
for(UIView *view in object.subviews) {
if([view.accessibilityIdentifier isEqual:@"my_location"] ) {
frame = view.frame;
}
}
}
}
使用调试查看正确的帧!!!
2) 不要使用 self.mapView.settings.myLocationButton = YES;
3)为此框架创建自己的按钮:
UIButton *buttonLocation = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonLocation.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 66, [UIScreen mainScreen].bounds.size.height - frame.size.height, frame.size.width, frame.size.height);
[buttonLocation addTarget:self
action:@selector(bLocations:)
forControlEvents:UIControlEventTouchUpInside];
[buttonLocation setImage:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[self.mapView addSubview:buttonLocation];
于 2017-07-05T13:22:58.033 回答
1
继之前的答案(使用从 GMSMapView 获取 UIButton 引用的方法)之后,这里是 Swift 中的更新答案。它将默认的“我的位置”图标替换为您自己的自定义图像。
private func setupMyLocationButton() {
let locationButton = mapView.subviews
.filter { $0.description.contains("GMSUISettingsPaddingView") }
.flatMap { $0.subviews }
.flatMap { $0.subviews }
.filter { $0.description.contains("GMSx_QTMButton") }
.first
let customImage = UIImage(imageLiteralResourceName: "yourCustomIconFilename")
let myLocationButton = locationButton as? UIButton
myLocationButton?.setImage(customImage, for: .normal)
}
于 2019-10-23T19:47:51.423 回答
0
为此,您可以从 GMSMapView 中检索对该按钮的 UIButton 引用。之后,您只需将标准 [UIButton setImage:] 用于所有不同的状态。我创建了一个从 GMSMapView 控件中获取此 UIButton 的类别:
于 2014-11-05T12:44:40.050 回答