1

我正在创建一个带有情节提要的标签栏应用程序。

基本上在 SecondViewController 上,我设置了一个 MKMapView,顶部有一个工具栏,用于更改视图等。

我设置了该跨度并添加了一个显示加载屏幕时的注释,它在使用 AutoLayout:Off 时正确显示,如下所示

(对不起,我不能在这里嵌入链接,因为我是新来的)

http://img211.imageshack.us/img211/9359/mvautooff1.jpg

当我将 AutoLayout 设置为 On 时,它会执行此操作

http://img153.imageshack.us/img153/3736/mvautoon.jpg

我试过改变跨度等,运行模拟器时没有任何变化。

如何更改它,以便 MapView 像 AutoLayout 关闭时一样显示?

有人可以帮我吗,因为我喜欢 AutoLayout 在它为设备等调整大小时打开,但我需要 MapView 来显示 AutoLayout 关闭时它是如何工作的。

我对 ios 编码非常陌生,完全是新手

任何帮助将不胜感激!

谢谢

.m 的代码是 -

#import "SecondViewController.h"
#import "Annotation.h"

@interface SecondViewController ()

@end

//Coordinates of Salon
#define SALON_LATITUDE -33.427528;
#define SALON_LONGITUDE 151.341697;

//Span
#define THE_SPAN 0.005f;

@implementation SecondViewController
@synthesize myMapView;

//Find my location button
-(IBAction)findmylocation:(id)sender {

myMapView.showsUserLocation = YES;
myMapView.delegate = self;
[myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

//Set map type button
-(IBAction)setmaptype:(id)sender {

switch (((UISegmentedControl *)sender).selectedSegmentIndex) {

    case 0:
        myMapView.mapType = MKMapTypeStandard;
        break;
    case 1:
        myMapView.mapType = MKMapTypeHybrid;
        break;
    case 2:
        myMapView.mapType = MKMapTypeSatellite;
        break;

    default:
        myMapView.mapType = MKMapTypeStandard;
        break;  
}
}

- (void)viewDidLoad
{
[super viewDidLoad];

//Create the region
MKCoordinateRegion myRegion;

//Center
CLLocationCoordinate2D center;
center.latitude = SALON_LATITUDE;
center.longitude = SALON_LONGITUDE;

//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;

myRegion.center = center;
myRegion.span = span;

//Set our mapView
[myMapView setRegion:myRegion animated:YES];


//Annotation

//1. Create a coordinate for use with the annotation
CLLocationCoordinate2D salonLocation;
salonLocation.latitude = SALON_LATITUDE;
salonLocation.longitude = SALON_LONGITUDE;


Annotation * myAnnotation = [Annotation alloc];
myAnnotation.coordinate = salonLocation;
myAnnotation.title = @"Generic Haircuts";
myAnnotation.subtitle = @"8888 Mann St, Gosford, 2250";

[self.myMapView addAnnotation:myAnnotation];

//Automatic annotation - CUSTOM CODE
[self.myMapView selectAnnotation:myAnnotation animated:YES];

}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

2

尝试调用 viewDidAppear 中的 setRegion 方法并将其从 viewDidLoad 中移除:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    //Create the region
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = SALON_LATITUDE;
    center.longitude = SALON_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;

    //Set our mapView
    [self.myMapView setRegion:myRegion animated:YES];
}

基本上使用自动布局,地图视图的框架尚未在 viewDidLoad 中设置:

iOS AutoLayout - 获取帧大小宽度

因此 setRegion 不能正常工作。

MKMapView:setRegion 不起作用!

于 2013-05-20T03:50:08.040 回答