【发布时间】:2013-06-19 03:13:51
【问题描述】:
有人知道如何从 GMSMapView 实例中获取 myLocationButton 的实例吗?或者改变默认位置的方法?我只需要将它向上移动一些像素。
【问题讨论】:
有人知道如何从 GMSMapView 实例中获取 myLocationButton 的实例吗?或者改变默认位置的方法?我只需要将它向上移动一些像素。
【问题讨论】:
根据Issue 5864: Bug: GMSMapView Padding appears not to work with AutoLayout 的问题跟踪器,有一个更简单的解决方法:
- (void)viewDidAppear:(BOOL)animated {
// This padding will be observed by the mapView
_mapView.padding = UIEdgeInsetsMake(64, 0, 64, 0);
}
【讨论】:
Raspu 的解决方案移动了包括指南针在内的整个 GMSUISettingsView。
我找到了只移动位置按钮的解决方案:
for (UIView *object in mapView_.subviews) {
if([[[object class] description] isEqualToString:@"GMSUISettingsView"] )
{
for(UIView *view in object.subviews) {
if([[[view class] description] isEqualToString:@"UIButton"] ) {
CGRect frame = view.frame;
frame.origin.y -= 75;
view.frame = frame;
}
}
/*
CGRect frame = object.frame;
frame.origin.y += 75;
object.frame = frame;
*/
}
};
如果取消注释这三行,则仅移动指南针(由于某种原因,我无法移动 GMSCompassButton 视图)。
【讨论】:
Swift 2.3: 仅移动位置按钮的解决方案。我正在使用 pod 'GoogleMaps', '~> 2.1'。
for object in mapView.subviews {
if object.theClassName == "GMSUISettingsView" {
for view in object.subviews {
if view.theClassName == "GMSx_QTMButton" {
var frame = view.frame
frame.origin.y = frame.origin.y - 110px // Move the button 110 up
view.frame = frame
}
}
}
}
获取类名的扩展。
extension NSObject {
var theClassName: String {
return NSStringFromClass(self.dynamicType)
}
}
我会尽快将此代码更新到 Swift 3.0。 :)
【讨论】:
目前我发现的唯一方法是这样:
//The method 'each' is part of Objective Sugar
[googleMapView.subviews each:^(UIView *object) {
if([[[object class] description] isEqualToString:@"GMSUISettingsView"] )
{
CGPoint center = object.center;
center.y -= 40; //Let's move it 40px up
object.center = center;
}
}];
它工作正常,但官方方式会更好。
这适用于 1.4.0 版本。对于以前的版本,将 @"GMSUISettingsView" 更改为 @"UIButton"。
【讨论】: