title: Dynamic View Controller
body: <p>I just need to find out several things about iOS views and layout. I have just ended application where I had to create a controller that show test. It was easy except one thing. There was two types of question A and B. A questions have 4 variants of answers, B questions don't. So I had to generate all my layout in code depends on question type. I've done it by creating method <code>update</code> where i rebuild my views. </p>
  
  <pre><code>- (void)update {
      for (UIView *v in self.view.subviews) {
          [v removeFromSuperview];
      }
      //label initialization with frame calculation
      UILabel *questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(PADDING,       PADDING, size.width - 4 * PADDING, 0)];
      [questionLabel sizeToFit];
      [self.view addSubview:questionContainerView];
  
      //If type A
      if ([self.question.questionType isEqualToString:@"A"]) {
          for (Answer *a in self.question.answers) {
              UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(PADDING, offset, size.width - 2 * PADDING, 30 + PADDING)];
              //customization
              [self.view addSubview:button];
              offset += button.frame.size.height + MARGIN_BOTTOM;
          }
          //if B type
      } else {
          UITextField *answerField = [[UITextField alloc] initWithFrame:CGRectMake(PADDING, PADDING, view.frame.size.width - 2*PADDING, view.frame.size.height - 2 * PADDING)];
          [view addSubview:answerField];
          offset += view.frame.size.height + MARGIN_BOTTOM;
      }
      UIView *lastView = [[self.view subviews]lastObject];
      self.scrollView.contentSize = CGSizeMake(size.width, lastView.frame.origin.y + lastView.frame.size.height + 10);
  }
  </code></pre>
  
  <p>And after all I think it's can't be so sophisticated. Please help to find out how should I do it properly, because I am sure my method is not optimal. Maybe create custom layout. I mean how to avoid setting frame and simply add views which take right position.</p>