title: Programatically adding views to the view controller's view using AutoLayout
body: <p>I have a view controller which I have created using code (no interface builder or storyboard). I am trying to programatically add few views (buttons and image views) and I am using Masonry for AutoLayout.</p>
  
  <p>I have the following code</p>
  
  <pre><code>- (instancetype)init {
      self = [super init];
  
      if (self) {
          self.view.backgroundColor = UIColor.whiteColor;
          self.progress = 0.f;
          self.shouldRecord = YES;
          [self createMainUI];
          [self addActions];
      }
      return self;
  }
  
  - (void)createMainUI {
      self.backButton = [self.view createAndAddSubView:UIButton.class];
      [self.backButton setImage:[TeamieUIGlobals defaultPicForPurpose:@"close" withSize:25.f] forState:UIControlStateNormal];
      [self.backButton addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
  
      self.micNormalImageView = [self.view createAndAddSubView:UIImageView.class];
  
      self.dynamicProgress = [self.view createAndAddSubView:TMEProgressImageView.class];
      self.dynamicProgress.image = IMAGE_MIC_WAVE;
      self.dynamicProgress.progress = 0;
      self.dynamicProgress.hasGrayscaleBackground = NO;
      self.dynamicProgress.verticalProgress = YES;
  
      self.recordButton = [self.view createAndAddSubView:UIButton.class];
      self.recordButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      self.recordButton.backgroundColor = UIColor.redColor;
      self.recordButton.layer.cornerRadius = 50.f;
      self.recordButton.titleLabel.font = [TeamieGlobals appFontFor:@"regularFontWithSize13"];
      [self.recordButton setTitle:@"Record" forState:UIControlStateNormal];
      [self.recordButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
  
      [self.backButton mas_makeConstraints:^(MASConstraintMaker *make){
          make.width.height.equalTo(@25);
          make.left.top.equalTo(@10);
      }];
      [self.micNormalImageView mas_makeConstraints:^(MASConstraintMaker *make){
          make.width.equalTo(@204);
          make.height.equalTo(@295);
          make.center.equalTo(self.view);
      }];
      [self.dynamicProgress mas_makeConstraints:^(MASConstraintMaker *make){
          make.width.equalTo(@204);
          make.height.equalTo(@295);
          make.center.equalTo(self.view);
      }];
      [self.recordButton mas_makeConstraints:^(MASConstraintMaker *make){
          make.width.height.equalTo(@100);
          make.centerX.equalTo(self.view);
          make.bottom.equalTo(self.view.mas_bottom).with.offset(15.f);
      }];
  }
  </code></pre>
  
  <p>This is what <code>createAndAddSubView</code> does</p>
  
  <pre><code>- (id)createAndAddSubView:(Class)modelClass {
      id temporaryView = [modelClass new];
      [self insertSubview:(UIView *)temporaryView atIndex:0];
      return temporaryView;
  }
  </code></pre>
  
  <p>The only view which appears on the view in the simulator is the <code>backButton</code> and no other view appears.</p>
  
  <p>Also, the last 2 constraints added to the <code>recordButton</code> crashes the app and the error is "<code>couldn't find a common superview for &lt;UIButton: 0x7ff2eb15f110; frame = (0 0; 0 0); opaque = NO; layer = &lt;CALayer: 0x7ff2eb118c60&gt;&gt; and &lt;UIView: 0x7ff2e8f4bf20; frame = (0 0; 320 568); autoresize = W+H; layer = &lt;CALayer: 0x7ff2e8fdf220&gt;&gt;</code>"
  but when I pin the button, there is no error but still it is not visible on the view. Any idea why the views aren't appearing, something wrong with the code?</p>