iOS mapview自动布局问题

时间:2013-05-19 08:08:18

标签: xcode storyboard mkmapview autolayout

我正在创建一个带有故事板的标签栏应用程序。

基本上在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

1 个答案:

答案 0 :(得分: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 - get frame size width

因此setRegion无法正常工作。

MKMapView : setRegion doesn't work !