Pages

Sunday, October 17, 2010

Iphone: Change view to landscape force fully by NavigationController

HI...... to All
In this, we are learn something new for iphone view and how to change it
iphone has two view: Portrait and LandScape.

Normally, we are using setOrientation Method to set the iphone View type.
such as follow.......

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft];
[self shouldAutorotateToInterfaceOrientation:UIDeviceOrientationLandscapeLeft];

then implement it method name as shouldAutorotateToInterfaceOrientation

such as
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation==UIInterfaceOrientationPortrait)
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
return (interfaceOrientation == UIDeviceOrientationLandscapeLeft);
}


But that whole work can be done without that by using status Bar and
it also so simple to do that. In whole process, we use the navigationcontroller with its transform,bounds and center value.

here i add sample code here, get it and happy programming

first define

#define degreesToRadian(x) (M_PI * (x) / 180.0)


and then use following code in viewWillAppear

if(orientation == UIInterfaceOrientationPortrait)
{

_originalTransform = [[appDelegate navigationController].view transform];
_originalBounds = [[appDelegate navigationController].view bounds];
_originalCenter = [[appDelegate navigationController].view center];
nav_frame = [appDelegate navigationController].navigationBar.frame;


// Rotate the view 90 degrees around its new center point.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);

[[appDelegate navigationController].view setTransform:landscapeTransform];

[appDelegate navigationController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[appDelegate navigationController].view.bounds = CGRectMake(0.0, 20.0, 480.0, 320.0);
[appDelegate navigationController].view.center = CGPointMake (240.0, 160.0);
[[appDelegate navigationController].navigationBar setBarStyle:UIBarButtonSystemItemFlexibleSpace ];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;

}

its work fine but when we will move from that view the other view is also show in landscape mode
So To solve that problem we again set the navigationController Properties in viewWillDisappear


use following Code from here:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];

[[appDelegate navigationController].view setTransform:_originalTransform];
[[appDelegate navigationController].view setBounds:_originalBounds];
[[appDelegate navigationController].view setCenter:_originalCenter];
[appDelegate navigationController].navigationBar.frame = nav_frame;
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;


Its Whole code, work fine and u can use it and feel something differ from running it.

Thanks

4 comments:

  1. Its all great helpful post.... keep continuing for easy learn and implement.....

    ReplyDelete
  2. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    ReplyDelete
  3. What is the reason for passing the values, +80 and +100, to CGAffineTransformTranslate? Why not some other value?

    ReplyDelete
  4. hi....Rachel,

    the passing values +80 and +100 depends on my screen size and it is not fixed so u can change it as u want.

    ReplyDelete