I have a TabBarController app with 4 views.
I've put on all the 4 viewControllers the autorotate method returning yes :
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
Thanks to this, now when i rotate the iPhone the rotation works, and even the TabBar is rotated.
In every single view i can use the following code to update to view when the rotation occurs:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) {
//Here i can update the view when it goes to landscape
}
else if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown) {
//Here i can update the view when it goes to portrait
}
}
The problem is that when i make the rotation, only the current displayed view is updating. If later i change view clicking on the tabBar, i see the view not updated for the new orientation.
Is there a way to make all the views being aware of the orientation changed?
Ok i found it! =D
The trick is using the function:
- (void)viewWillAppear:(BOOL)animated
Just call it in your viewControllers, and insert inside the code to update the view. Enjoy!
Related
I want to make an iPhone app which shows a view when iPhone is in portrait mode, and ANOTHER when iPhone is in landscape mode.
I know there is many post about that but I don't understant the answer.
In a first time, to understand, I make test with a Tabbed Application, because I have already two views. When I tap on the second screen, I would like my iphone in landscape mode.
(and in the first one in portrait mode).
On Apple website, and stackoverflow, I saw the following code :
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ((orientation == UIInterfaceOrientationPortrait) ||
(orientation == UIInterfaceOrientationLandscapeLeft))
return YES;
return NO;
}
Or a similar code.
In my mainstoryboard, I put the second view in landscape, with the interface.
But when I run my app, and I tap on second screen, iPhone stay in portrait mode..
I tried to do the same thing with a single view app, and created new file (landscapeViewController) with .xib file, but I can't have a godd result!
First, in storyboard, create segues from your portrait view controller to your landscape view controller and vice-versa. Then, in your portrait view controller, do this:
- (void)viewWillLayoutSubviews
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[self performSegueWithIdentifier:#"SegueToLandscapeViewController" sender:self];
}
}
In your landscape view controller, do this:
- (void)viewWillLayoutSubviews
{
if (!UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[self performSegueWithIdentifier:#"SegueToPortraitViewController" sender:self];
}
}
I succeeded with the following code :
-(void)orientationDidChanged: (NSNotification *)notification {
UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(devOrientation)) {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
landscapeViewController *landscape = [main instantiateViewControllerWithIdentifier:#"landscape"];
[self presentViewController:landscape animated:YES completion:nil];
}
else if (UIDeviceOrientationIsPortrait(devOrientation)) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
But I can't change transition between View Controller, whereas I can between simple View.
Maybe it's because I have two views controllers linked to the same view controller.h...?
The root view controller of navigation controller supports only portrait orientation and other controllers supports all orientation.Now if i am on the root view controller and the DEVICES is in landscape and if i push next view controller that opens in portrait that should open in landscape as it supports all orientation.
Please help me with this.
Using iPhone 4s iOS6.1.3
you can check Device orientation in your first screen after login viewcontroller using bellow code:-
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
sor when you load this viewcontroller it check first device oriantation and then load it's related frame
I think this is the issue related to the orientation changes in iOS6. You need to subclass the UINavigationController
Check this
1 . You have to create sub class of UINavigationController. add Following method.. Take one boolean variable to check whether it support for all orientation or not and change its value.
#implementation NavigationControllerViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
AppDelegate *appdelgate=[[UIApplication sharedApplication]delegate];
if (appdelgate.issuppoertAll) {
// for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
#end
2 when you navigate form root view controller to other view controller
use this code , when you want to forcefully change its orientation.i.e lanscape to portrait
obj_viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:obj_viewcontroller animated:NO];
[self dismissModalViewControllerAnimated:NO];
[self.navigationController pushViewController:obj_viewcontroller animated:NO];
3 In second view controller you have to change boolean variable value
-(void)viewWillAppear:(BOOL)animated
{
appdelgate.issuppoertAll=YES;
}
4 Add this method into all view controller and set orientation as per your need.
- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
In iOS, after dismissing a modal view controller (which is being forced to only show in portrait orientation), if the device is being held in landscape mode, the view controller which presented the modal view controller doesn't rotate back to landscape correctly.
The 'willAnimateRotationToInterfaceOrientation' method gets called so I can manage rotation of my subviews etc. but then the actual view controller doesn't rotate to landscape. So my subviews look like they should in landscape, but the interface is in portrait mode.
The annoying thing is that it works fine in the iOS 5 simulator. Ie. After dismissing the modal view controller, the presenting view controller rotates back to landscape orientation.
Has anyone experienced something similar, or have any idea how to approach this?
For IOS 6 u need to handle the orientation with this methods.
- (BOOL)shouldAutorotate; {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
else{
return UIInterfaceOrientationMaskLandscape;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return UIInterfaceOrientationPortrait;
}
else{
return UIInterfaceOrientationLandscapeLeft;
}
}
So I have a UITableViewControler displaying a tableview in portrait mode.
As soon as i rotate the iPhone i want to present a modal view in landscape mode.
In the tableView i use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
And to handle the present the modal view:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
{
NSLog(#"Push page view");
PagingViewController *s = [[PagingViewController alloc] initWithNibName:#"PagingView" bundle:nil];
s.hidesBottomBarWhenPushed = YES;
[self presentModalViewController:s animated:YES];
[s release];
}
}
The modal view i have the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
And to dismiss the modal view it self, I do:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
NSLog(#"Dismiss my self");
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
}
Some how this works two times.
The third time i rotate the iPhone from Portrait mode to Landscape mode, i get a bad access error.
I cant figure out what gives me the error.
Anyone care for a shot?
The simplest way I can think of is to implement -shouldAutorotate... and dismiss the modal view and return NO to abort rotation. Perhaps that will be sufficient to avoid any concurrency issues. If this suggestion isn't to your liking take a look at NSNotificationCenter.
I'm working on an app that will need to display a coverflow style view in landscape when the device is rotated to landscape orientation. I've worked on a few apps before, but none of them required landscape/rotation so I'm not experienced with it. I have the code to draw the coverflow view, but presenting it is proving tough.
What I'd like is basically like what the iPod app does when displaying coverflow. The view underneath does not rotate, but the coverflow fades in on top, and fades out when rotated back to portrait.
I'm guessing it's something to do with shouldAutorotateToInterfaceOrientation and a modal view being presented with a fade transition, or using the technique found here:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/22285-replicating-cool-ipod-landscape-transition-iphone.html
I guess my main problem is that the modal view is presented, but it's contents are not rotated to landscape. What should I do?
Here's the code I'm using now:
PARENT VIEW
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
NSLog(#"rotating?");
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
NSLog(#"rotating");
CoverFlowViewController *coverFlowView = [[CoverFlowViewController alloc] init];
[coverFlowView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:coverFlowView animated:YES];
[coverFlowView release];
}
return YES;
}
MODAL VIEW
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSLog(#"rotating? going back?");
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
return YES;
} else {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
return NO;
}
You should do the transition inside:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}