Search code examples
iphoneiosuitableviewdevice-orientation

UITableView does not change it size correctly on device orientation change


This may sound a newbie question, however I'm new to iOS dev,

I've UIWebView and UITableView on my iPad view. In shouldAutorotateToInterfaceOrientation I resize them for nice look like this.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {    
        if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTextView.frame = f;
            f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTableView.frame = f;
        }
        if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTextView.frame = f;
            f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTableView.frame = f;        
        }   
        return YES;
    }

Now the question: For the first load the table view is drawn with correct sizes but when I switch to portrait mode then back to landscape mode the UITableView becomes wider than specified by frame. So why this happens and how to fix this ?

PS. I've tried also to set the frames for all cells in the same method didn't help.

PSS. This happens only on device, on simulator UITableView is displayed correctly


Solution

  • shouldAutorotateToInterfaceOrientation: only returns a boolean value indicating whether the view controller supports the specified orientation, you should not do any UI transformation in this method, instead do everything in willAnimateRotationToInterfaceOrientation:duration:.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }
    
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
    {    
        if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTextView.frame = f;
            f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTableView.frame = f;
        }
        if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTextView.frame = f;
            f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTableView.frame = f;        
        }
    }
    

    Have a look at the documentation for UIViewController, it's well explained.