rotating MKMapView

久未见 提交于 2019-12-10 11:28:56

问题


I have a working app with an MKMapView showing user location. So far, so good:

- (void)viewDidLoad {
   ...
   [myMapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
   [myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

Now, I allow interface rotations:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

If I now turn the device and center to the user location

   [myMapView setCenterCoordinate:[myMapView userLocation].coordinate animated:YES];

The mapView will rotate as expected, but the user location will not be centered, but on the bottom of the screen (distance from top stays the same as in portrait mode).

I expect it to be centered, though...

Any ideas?


回答1:


Try including height in the setAutoresizingMask: so that the map view's height will automatically change when the superview changes its height on rotation. Right now, only the width is resizing and so the y position of the map's center doesn't change.

So this line:

[myMapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

should be:

[myMapView setAutoresizingMask: 
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];


Additionally, you shouldn't need to manually center the map on the user location if you are setting the tracking mode to MKUserTrackingModeFollow (the map will do it automatically). You may have tried manually centering it to fix the height issue but fixing the autoresizing mask should eliminate the need for the explicit centering.



来源:https://stackoverflow.com/questions/10865945/rotating-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!