Reputation:
I am creating a view programmatically. The view is fairly simple as it only contains an MKMapView
which I want to fill the screen (with the exception of the navigation bar at the top).
Here is what I am doing to setup the MKMapView:
- (void)setupMapView
{
mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
mapView.delegate = self;
mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:mapView];
}
However, the MKMapView
doesn't seem to fill the entire screen. If you look at the top of this screenshot there is a gap between the top of the map and the bottom of the navigation bar (which is added by the UINavigationController
).
Please could someone suggest why this is happening and how I can fix it?
Upvotes: 1
Views: 1076
Reputation: 119021
The frame of the view (self.view.frame
) probably have an offset in the y
position. This is to position it correctly in its superviews coordinate space. When you copy that frame onto your new view it is inappropriate because you are moving into a different coordinate space.
A cheap alternative that will work in 99.9% or cases is to use the view bounds
instead:
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
Because this usually has an origin
of zero and the full size
of the view.
Upvotes: 3