Sean McQuarrie
Sean McQuarrie

Reputation: 23

iOS mapview auto layout issue

Im creating an tab bar app with a storyboard.

Basically on the SecondViewController, I set up an MKMapView with a toolbar at the top for changing views etc.

I set that span and added an annotation that shows when the screen is loaded, It shows up correctly when using AutoLayout:Off as shown below

(sorry I couldnt embed links as im new here)

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

When I put AutoLayout to On it does this

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

I have tried changing the span etc and nothing changes when running the simulator.

How can I change it so the MapView shows like it does when AutoLayout is off?

Can someone please help me as I like the AutoLayout to be on as it resizes for devices etc but I need the MapView to show how it does when AutoLayout is off.

I am very new to ios coding and am totally a newb

Any help will be greatly appreciated!

Thanks

The code for the .m is -

#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

Upvotes: 1

Views: 2462

Answers (1)

Valent Richie
Valent Richie

Reputation: 5226

Try to call the setRegion method in viewDidAppear and remove it from the 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];
}

Basically with Auto Layout, the frame of the map view is not set yet in viewDidLoad:

iOS AutoLayout - get frame size width

Hence the setRegion can not work properly.

MKMapView : setRegion doesn't work !

Upvotes: 2

Related Questions