iPhone Development
Views » Landscape[« back]
» Force landscape mode in one viewFirst in the interface:
@interface YourViewController : UIViewController{ UITableView *myTableView; CGAffineTransform _originalTransform; CGRect _originalBounds; CGPoint _originalCenter; } @property (nonatomic, retain) UITableView *myTableView; @end
Next here is the implementation:
@implementation YourViewController
@synthesize myTableView;
- (void)loadView {
UIView *viewContainer = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
CGRect tableFrame;
tableFrame.origin.x += 10;
tableFrame.origin.y -= 15;
tableFrame.size.height = 320;
tableFrame.size.width = 460;
// create and configure the table view
myTableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.autoresizesSubviews = YES;
myTableView.scrollEnabled = YES;
myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[viewContainer addSubview:myTableView];
self.view = viewContainer;
}
- (void)viewWillAppear:(BOOL)animated {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
_originalTransform = [[appDelegate navController].view transform];
_originalBounds = [[appDelegate navController].view bounds];
_originalCenter = [[appDelegate navController].view center];
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);
[[appDelegate navController].view setTransform:landscapeTransform];
[appDelegate navController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[appDelegate navController].view.bounds = CGRectMake(0.0, 0.0, 480.0, 320.0);
[appDelegate navController].view.center = CGPointMake (240.0, 160.0);
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}
- (void) viewWillDisappear:(BOOL)animated {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate navController].view setTransform:_originalTransform];
[[appDelegate navController].view setBounds:_originalBounds];
[[appDelegate navController].view setCenter:_originalCenter];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}
As you can see we are manually transforming the views and restoring the correct views. There is one problem interface bug when you leave this view, but nothing major.
Credit
» Link


