Search code examples
iosobjective-cmasonry-ios-osx

How to use iOS Masonry (Auto Layout) to position two buttons next to each other


I am updating a screen which initially had one button at the bottom. My task is to render two buttons centered at the bottom using Masonry framework which is an AutoLayout wrapper.

I would like to have the two buttons centered at the bottom and side by side. This is what I've come up with:

enter image description here

Here's the code:

- (void)createConstraints {

    // Map View
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(self);
        make.bottom.equalTo(self.mas_centerY).multipliedBy(0.9);
    }];

    // Information TextView
    [self.informationTextView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.mapView.mas_bottom); //.with.offset(kTableViewCellPadding);
        make.left.and.right.equalTo(self);
    make.bottom.equalTo(self.editSiteButtonBackground.mas_top);//.with.offset(-kTableViewCellPadding);
    }];

    // Edit  Site Button Background
    [self.editSiteButtonBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.and.right.equalTo(self);
        make.height.equalTo(@54);
    }];

  // Add  Site Comments Button Background
  [self.addSiteCommentsButtonBackground mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.editSiteButton.mas_right);
    make.bottom.equalTo(self.editSiteButton);
    make.height.equalTo(@54);
  }];

    // Edit  Site Button
    [self.editSiteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
        make.edges.equalTo(self.editSiteButtonBackground).with.insets(padding).with.priorityHigh();
        make.width.lessThanOrEqualTo(@260);
        make.centerX.equalTo(self.editSiteButtonBackground);
    }];

  // Add  Site Comments Button
  [self.addSiteCommentsButton mas_makeConstraints:^(MASConstraintMaker *make) {
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
        make.edges.equalTo(self.addSiteCommentsButtonBackground).with.insets(padding).with.priorityHigh();
    make.width.lessThanOrEqualTo(@270);
    make.top.equalTo(self.addSiteCommentsButton);
    make.centerX.equalTo(self.addSiteCommentsButtonBackground);
  }];

  // Navigation
  [self.navigationButton mas_makeConstraints:^(MASConstraintMaker *make) {
      make.right.equalTo(self.mapView).with.offset(-20);
      make.bottom.equalTo(self.mapView).with.offset(-20);
  }];

}

How do I show the buttons side-by-side and centred at the bottom?


Solution

  • Here's one possible solution:

    - (void)createConstraints {
    
        // Map View
        [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.and.right.equalTo(self);
            make.bottom.equalTo(self.mas_centerY).multipliedBy(0.9);
        }];
    
        // Information TextView
        [self.informationTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mapView.mas_bottom); //.with.offset(kTableViewCellPadding);
            make.left.and.right.equalTo(self);
        make.bottom.equalTo(self.editSiteButtonBackground.mas_top);//.with.offset(-kTableViewCellPadding);
        }];
    
        // Edit  Site Button Background
        [self.editSiteButtonBackground mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.and.right.equalTo(self);
            make.height.equalTo(@54);
        }];
    
      // Add  Site Comments Button Background
      [self.addSiteCommentsButtonBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.editSiteButton.mas_right);
        make.bottom.equalTo(self);
        make.height.equalTo(@54);
      }];
    
        // Edit  Site Button
        [self.editSiteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
            make.edges.equalTo(self.editSiteButtonBackground).with.insets(padding).with.priorityHigh();
            make.width.lessThanOrEqualTo(@260);
            make.centerX.equalTo(self).multipliedBy(0.5);
        }];
    
      // Add  Site Comments Button
      [self.addSiteCommentsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
        make.edges.equalTo(self.addSiteCommentsButtonBackground).with.insets(padding).with.priorityHigh();
        make.width.lessThanOrEqualTo(@260);
        make.top.equalTo(self.editSiteButton);
        make.centerX.equalTo(self).multipliedBy(1.5);
      }];
    
      // Navigation
      [self.navigationButton mas_makeConstraints:^(MASConstraintMaker *make) {
          make.right.equalTo(self.mapView).with.offset(-20);
          make.bottom.equalTo(self.mapView).with.offset(-20);
      }];
    
    }