Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically resize mkmapview to fill entire screen on orientation change?

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

    self.mapView.frame = self.view.bounds;
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(36.7472, -95.9594);
    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 10000*1609.344, 10000*1609.344)];
    [self.mapView setRegion:adjustedRegion animated:YES];
}

That's the code I have right now. When I change the orientation of my iPad from landscape to portrait, the map view's size doesn't change. I would like it to fill the entire view. I have tried searching google and stackoverflow for a solution, but I haven't had any luck.

Any help would be immensely appreciated.

like image 480
Beebunny Avatar asked Dec 04 '22 08:12

Beebunny


1 Answers

-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.mapView.frame = self.view.bounds;
}

This will ensure that the map frame will adjust to the view bounds whenever the view controller layout is triggered (like, on autorotation).

You can actually take the set frame line out of viewWillAppear, since viewWillLayoutSubviews runs at initial setup time.

like image 52
jsd Avatar answered Jan 01 '23 07:01

jsd