问题
Anybody know how to get the instance of the myLocationButton from a GMSMapView instance? Or a way to change the default position? I just need to move it some pixels up.
回答1:
According the the issue tracker for this Issue 5864: Bug: GMSMapView Padding appears not to work with AutoLayout there is an easier workaround:
- (void)viewDidAppear:(BOOL)animated {
// This padding will be observed by the mapView
_mapView.padding = UIEdgeInsetsMake(64, 0, 64, 0);
}
回答2:
Raspu's solution moves the whole GMSUISettingsView including the compass.
I found a solution to move only the Location Button:
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;
*/
}
};
If you uncomment the three lines you move only the compass (for some reason I'm not able to move the GMSCompassButton View).
回答3:
Swift 2.3: Solution to move only the Location Button. I am using 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 to get the Class Name.
extension NSObject {
var theClassName: String {
return NSStringFromClass(self.dynamicType)
}
}
I will update this code to Swift 3.0 as soon as possible. :)
回答4:
Right now the only way I have found is this way:
//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;
}
}];
It's working fine, but an official way would be better.
This works for 1.4.0 Version. For previous versions, change @"GMSUISettingsView"
for @"UIButton"
.
来源:https://stackoverflow.com/questions/17182308/how-to-change-mylocationbutton-position-from-a-gmsmapview