title: iOS: navigation bar's titleView doesn't resize correctly when phone rotates
body: <p>I'm writing an iPhone app that (like most apps) supports auto-rotation: You rotate your phone, and its views rotate and resize appropriately.</p>
  
  <p>But I am assigning a custom view to <code>navigationItem.titleView</code> (the title area of the navigation bar), and I can't get that view to resize correctly when the phone rotates.</p>
  
  <p>I know what you're thinking, "Just set its <code>autoresizingMask</code> to <code>UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight</code>," but it's not that simple.  Of course, if I don't set my view's <code>autoresizingMask</code>, then my view doesn't resize; and I want it to resize.</p>
  
  <p>The problem is, if I <em>do</em> set its <code>autoresizingMask</code>, then it resizes correctly as long as that view is visible; but the <code>titleView</code>'s size gets messed up in this scenario:</p>
  
  <ol>
  <li>Run the app, with the phone held in portrait mode.  Everything looks good.</li>
  <li>Do something that causes the app to push another view onto the navigation stack.  E.g. click a table row or button that causes a call to <code>[self.navigationController pushViewController:someOtherViewController animated:YES]</code>.</li>
  <li>While viewing the child controller, rotate the phone to landscape.</li>
  <li>Click the "Back" button to return to the top-level view.  At this point, the title view is messed up: Although you are holding the phone in landscape mode, the title view is still sized as if you were holding it in portrait mode.</li>
  <li>Finally, rotate the phone back to portrait mode.  Now things get even worse: The title view shrinks in size (since the navigation bar got smaller), but since it was already too small, now it is <em>much</em> too small.</li>
  </ol>
  
  <p>If you want to reproduce this yourself, follow these steps (this is a bit of work):</p>
  
  <ol>
  <li>Make an app using Xcode's "Navigation-based Application" wizard.</li>
  <li>Set it up so that the top-level table view has rows that, when you click them, push a detail view onto the navigation stack.</li>
  <li><p>Include this code in both the top-level view controller and the detail view controller:</p>
  
  <pre><code>- (BOOL)shouldAutorotateToInterfaceOrientation:
          (UIInterfaceOrientation)interfaceOrientation {
      return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  }
  </code></pre></li>
  <li><p>Include this code in only the top-level view controller:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      // Create "Back" button
      UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Master"
          style:UIBarButtonItemStylePlain target:nil action:nil];
      self.navigationItem.backBarButtonItem = backButton;
      [backButton release];
  
      // Create title view
      UILabel* titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,500,38)] autorelease];
      titleView.textAlignment = UITextAlignmentCenter;
      titleView.text = @"Watch this title view";
  
      // If I leave the following line turned on, then resizing of the title view
      // messes up if I:
      //
      // 1. Start at the master view (which uses this title view) in portrait
      // 2. Navigate to the detail view
      // 3. Rotate the phone to landscape
      // 4. Navigate back to the master view
      // 5. Rotate the phone back to portrait
      //
      // On the other hand, if I remove the following line, then I get a different
      // problem: The title view doesn't resize as I want it to when I:
      //
      // 1. Start at the master view (which uses this title view) in portrait
      // 2. Rotate the phone to landscape
      titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  
      self.navigationItem.titleView = titleView;
  }
  </code></pre></li>
  <li><p>Finally, follow my repro steps.</p></li>
  </ol>
  
  <p>So ... am I doing something wrong?  Is there a way to make my titleView always resize correctly?</p>