Saturday, February 11, 2012

Dynamically change the size of UITableView in iPad DetailViewController

If you add the UITableView to the UIView in iPad's DetailViewController,  the following code can change the dimension of UITableView based on interfaceOrientation.

in .h file

@interface DetailViewController : UIViewController < UITableViewDataSource, UITableViewDelegate, UIPopoverControllerDelegate, UISplitViewControllerDelegate>
{
    UITableView *myTableView;

}

in .m file

- (void)viewDidLoad
{


CGRect frame = [UIScreen mainScreen].applicationFrame;
    myTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    myTableView.delegate= self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];

[super viewDidLoad];

}





- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown
    {
        
        // Portrait
        CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height);
        myTableView.frame = frame;
        
    }
    else {
        
        // Landscape

        CGRect frame = CGRectMake(0, 0, 703, self.view.frame.size.height);
        myTableView.frame = frame;
       
    }
     
    return YES;
}

No comments:

Post a Comment