title: Autoresizing of a custom view in a scrollview
body: <p>I have created a custom view which is loaded from a xib file like so:</p>
  
  <pre><code>- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
      // Initialization code.
      [[NSBundle mainBundle] loadNibNamed:@"TachoView" owner:self options:nil];
      [self addSubview:self.view];
      [self initUi];
    }
    return self;
  }
  </code></pre>
  
  <p>I am now experiencing problems with the autoresizing and positioning of some subviews, when the device orientation changes. These screenshots describe the behaviour.</p>
  
  <p>After viewDidLoad everything looks fine:</p>
  
  <p><img src="https://i.stack.imgur.com/2PCFZ.png" alt="enter image description here"></p>
  
  <p>After the rotation to landscape mode, the layout is still fine:</p>
  
  <p><img src="https://i.stack.imgur.com/sbLyA.png" alt="enter image description here"></p>
  
  <p>But when rotating back to protrait, it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/zZI82.png" alt="enter image description here"></p>
  
  <p>The same files .h, .m and .xib work fine, if I test them in a single view dummy project. So I think, the settings for positioning and resizing in the xib are ok. The difference to my app is, that the view is positioned on a scrollview with several other views. On orientation change, I change the size of all these views, so that everything is layouted as expected. The relevant code is the innermost for-loop:</p>
  
  <pre><code>-(void) viewWillLayoutSubviews {
      float h = self.view.frame.size.height;
      float w = self.view.frame.size.width;
  
      // set the size of the vertical scrollview
      verticalView.frame = CGRectMake(0, 0, w, h * gridHeight);
      verticalScrollView.frame = CGRectMake(0, 0, w, h);
      [verticalScrollView setContentSize:CGSizeMake(w, h * gridHeight)];
      [verticalScrollView layoutIfNeeded];
  
      // set the size of the horizontal scrollviews
      for (int i=0; i&lt;verticalView.subviews.count; i++) {
          UIScrollView *scrollView = (UIScrollView *) [verticalView.subviews objectAtIndex:i];
          UIView *container = (UIView *) [[scrollView subviews] objectAtIndex:0];
          int subviewCount = container.subviews.count;
          scrollView.frame = CGRectMake(0, h*i, w, h);
          [scrollView setContentSize:CGSizeMake(w * subviewCount, h)];
          container.frame = CGRectMake(0, 0, w * subviewCount, h);
  
          // set the size of the actual views
          for (int j=0; j&lt;container.subviews.count; j++) {
              UIView *view = [container.subviews objectAtIndex:j];
              // FIXME manchmal crashed hier die WebView. Muss das hier im
              // ui thread ausgeführt werden? wird diese methode nicht vom
              // ui thread aufgerufen?
              view.frame = CGRectMake(w*j, 0, w, h);
          }
      }
  }
  </code></pre>
  
  <p>Any idea, what's going wrong with the second orientation change? I could layout the whole view manually, but I think it is possible with the autoresizing functionality, too.</p>