Search code examples
iosobjective-cnslayoutconstraint

Program Subview's Vertical Spacing to Bottom Constraint


SecondViewController adds a UIView that contains a MKMapView as a subview inside an IBAction method:

if(_tagTwo == 4){
    seg2_buttonImg = @"Maps.png";

    UIImage *btnImage1 = [UIImage imageNamed:seg2_buttonImg];
    [_left_button setImage:btnImage1 forState:UIControlStateNormal];
    [_table removeFromSuperview];
    [mapVC layoutMapView];
    [self.view addSubview:mapVC.view];
    return;
}

mapVC is created in ViewDidLoad with mapVC = [[CollectionMapViewController alloc] init];

Edit: new code for adding constraints, taken from @Reinier Melian:

@implementation CollectionMapViewController

@synthesize mapView;

- (void)viewDidLoad
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        //[segmentedControl setBackgroundColor:[UIColor whiteColor]];
        //[segmentedControl setBackgroundColor:[UIColor leafletBrown]];
        [segmentedControl setBackgroundColor:[UIColor leafletLightBrown]];
        segmentedControl.layer.cornerRadius = 5;
    }
    self.mapView.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray * verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.mapView)];
    NSArray * horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mapView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.mapView)];


    //Add constraints to the Parent
    [self.view addConstraints:verticalConstraints];
    [self.view addConstraints:horizontalConstraints];
}

CollectionMapViewController.h:

@interface SecondViewController : UIViewController <NSFetchedResultsControllerDelegate, CollectionMapViewControllerDelegate>{

    CollectionMapViewController* mapVC;
}

@property (nonatomic, retain) CollectionMapViewController* mapVC;

The problem is that it adds the MKMapView to the top of the screen:

enter image description here

I tried to constrain the MKMapView to the bottom of the screen by adding following code to viewDidLoad of MapViewController

   mapView.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint *bottom =[NSLayoutConstraint
                                 constraintWithItem:mapView
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:self.view
                                 attribute:NSLayoutAttributeBottom
                                 multiplier:1.0f
                                 constant:0.f];


    //Add constraints to the Parent
    [self.view addConstraint:bottom];

But it just makes the map go black (or more likely moves it completely off the screen?).

enter image description here

What I am trying to get is this: enter image description here

I would appreciate any help! Thank you so much!


Solution

  • In viewDidLoad:

    mapView.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    
        /*Set width and height of mapview */
        NSLayoutConstraint *widthConst = [NSLayoutConstraint constraintWithItem:mapView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:400];
        NSLayoutConstraint *heightConst = [NSLayoutConstraint constraintWithItem:mapView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:300];
        [self.view addConstraint:widthConst];
        [self.view addConstraint:heightConst];
    
    
        /*creater X and Y constraints for mapview */
        NSLayoutConstraint *centreHorizontallyConstraint = [NSLayoutConstraint
                                                            constraintWithItem:mapView
                                                            attribute:NSLayoutAttributeCenterX
                                                            relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                            attribute:NSLayoutAttributeCenterX
                                                            multiplier:1.0
                                                            constant:0];
    
        [self.view addConstraint:centreHorizontallyConstraint];
    
        NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint
                                                constraintWithItem:mapView
                                                attribute:NSLayoutAttributeBottom
                                                relatedBy:NSLayoutRelationEqual
                                                toItem:self.view
                                                attribute:NSLayoutAttributeBottom
                                                multiplier:1.0
                                                constant:140];
    
        [self.view addConstraint:bottomConstraint];