A couple of thoughts:
I'd set
translatesAutoresizingMaskIntoConstraints
toNO
.You can probably simplify your VFL, too, and eliminate the
>=0
stuff. You can also set the width and height right in your VFL.You also don't need to set the
frame
. Let the constraints set the width, height.Unless you're doing this in
loadView
, I'd suggest you add the map as a subview rather than trying to setself.view
.
Thus:
_map = [[MKMapView alloc] init];
_map.delegate = self; // if you've implemented any `MKMapViewDelegate` methods, you should set your delegate
[_map setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_map];
NSDictionary *views = @{@"map" : _map};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[map]|"
options:0
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[map]|"
options:0
metrics:nil
views:views]];
// do your stuff where you're adding your annotation here
_directionsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_directionsButton addTarget:self action:@selector(getLocation:) forControlEvents:UIControlEventTouchDown];
[_directionsButton setTitle:@"Get Directions" forState:UIControlStateNormal];
[_directionsButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self view] addSubview:_directionsButton];
NSDictionary *nameMap = @{@"button" : _directionsButton};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button(150)]"
options:0
metrics:nil
views:nameMap];
[[self view] addConstraints:horizontalConstraints];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(25)]-10-|"
options:0
metrics:nil
views:nameMap];
[[self view] addConstraints:verticalConstraints];
You might even want to set the priority for the width and height constraints to be less than 1000, that way if the font dictates that the button should be a little larger, you'll let it grow, e.g. @"H:|-10-[button(150@500)]"
and @"V:[button(25@500)]-10-|"
.