Change views when iPhone is rotated? - objective-c

How would I have one view up when the iPhone is in a standard 'portrait' orientation, and switch to a different view (say a graph or something) when rotated to a landscape orientation and visa versa?

Disable orientation for that view (assuming that the first view is landscape)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight); }
Then add this to the viewDidAppear
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
And add this method somewhere
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
// Present View Controller here
break;
default:
break;
};
}
And on the other view do the same but backwards with a dismiss for landscape instead of the present for portrait.
Dont forget to unregister for the notifications.
(alternatively use a navigation view with both controls but without the bar and simply show the one you want depending on the orientation using)
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration

First read how UIViewControllers work:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
Then, in your UIViewController subclass, take advantage of willRotateToInterfaceOrientation:duration: to change out your views.
e.g.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// if portrait
[self.landscapeView removeFromSuperview];
[self.view addSubview:self.portraitView];
// if landscape
[self.portraitView removeFromSuperview];
[self.view addSubview:self.landscapeView];
}
And add in the appropriate if statement or switch case to determine which to do.

Related

iPad Landscape orientation

I am creating an App in portrait and Landscape Mode for iPad . I create a XIB in Landscape Mode for iPad but when i run that App it always shows in portrait Mode .
I set all setting under property list(.plist) file as "Supported Interface orientation(iPad)" and set Landscape(Left Home button) and landscape(Right Home Button) and also check the Orientation from the Code but all this doesn't work.
please help us if any one knows the exact problem or this , we are using Navigation Controller
Add this line of code in didFinishLaunchingWithOptions in delegate.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(preferredInterfaceOrientationForPresentation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Then add following methods in delegate first and run the app, if fixed, COOOOL else add following four methods in each your view controller. You problem will be fixed.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}

iOS: Change interface orientation when pop view controller

All view controllers in my app are working only in portrait orientation except one which can be portrait or landscape oriented.
I have some usage scenario like following:
I push controller which works in both orientations to UITabBarController
User change orientation from portait to landscape
User press "back button"
After these actions application remains in landscape orientation and does not change it automatically to portrait.
I control view controller orientation using supportedInterfaceOrientations (I use iOS 6.0). What I do wrong? How can I get correct behaviour when application automatically change orientation to allowed when user press back button? Thank you for answer!
In iOS 6 (and possibly earlier), if a view controller is offscreen when the device rotates, it does not get any notification. Nor does it get sent willAnimateRotationToInterfaceOrientation:duration: when it becomes the top view controller.
You need to keep track of the current orientation of the view controller and check the device orientation in viewWillAppear:. If they are different, you can use willAnimateRotationToInterfaceOrientation:duration: to set it correctly.
Since this is something you are likely to do a lot, you may want to create a generic superclass that your view controllers inherit from.
A typical solutions is:
#implementation MyHandlesOffscreenRotationController
{
BOOL isShowingPortrait;
}
- (void) viewDidLoad
{
[super viewDidLoad];
isShowingPortrait = UIInterfaceOrientationIsPortrait(
[[UIApplication sharedApplication] statusBarOrientation]);
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
BOOL currIsPortrait = UIInterfaceOrientationIsPortrait(
[[UIApplication sharedApplication] statusBarOrientation]);
if ((isShowingPortrait && !currIsPortrait) ||
(!isShowingPortrait && currIsPortrait)) {
[self willAnimateRotationToInterfaceOrientation:
[[UIApplication sharedApplication] statusBarOrientation]
duration:0.0f];
}
}
#end
Just override -(BOOL)shouldAutoRotate and - (NSUInteger)supportedInterfaceOrientations inside a UINavigationController category, then ViewController will force rotate to its supported orientation after pop from other ViewController.
#implementation UINavigationController (Rotate)
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
#end
iOS 9 and above
At the time of pop just write the below-mentioned code in your viewWillAppear method.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:#"orientation"];
With this, your view will appear in portrait mode.
P.L. has a nice solution in this topic: Present and instantly dismiss an empty modal view controller which allows only portrait | landscape
How to change the device orientation programmatically in iOS 6

Supporting two orientations with separate view controllers in iOS

I'm in a real mess right now . I used apples sample code which does this :
Create a portrait view controller and a landscape view controller
Potrait event controller then registers for device orientation changed notifications
When device is rotated it presents a modal view controller for the landscape view or dismisses the landscape view if it is rotated back to portrait .
Everythings working as it should except for a little problem ....
Now to my problem . I used this to launch a rotatable view controller from a table view . It can be rotated and works fine . But if I initially launch it in landscape mode it will still launch as portrait . If i want landscape i have to afterwards AGAIN rotate it to landscape .I tried very hard to fix for this but failed . You can download and run the sample code from Apple Developer Site Here . Can anyone fix this code so that if launched in landscape mode it presents the modal view for the landscape view ? Otherwise I'll have to rewrite everything to use a single view controller .
These are the relevant portions of apples code :
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor colorWithRed:197.0/255.0 green:204.0/255.0 blue:211.0/255.0 alpha:1.0];
LandscapeViewController *viewController = [[LandscapeViewController alloc]
initWithNibName:#"LandscapeView" bundle:nil];
self.landscapeViewController = viewController;
[viewController release];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:#selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self presentModalViewController:self.landscapeViewController animated:YES];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return (interfaceOrientation == UIInterfaceOrientationPortrait); // support only portrait}
I know this is probably no longer relevant to you, but I just came across the same glitch and here's my solution.
From the way you have your code set out (including how Apple sets it out)
- (void)updateLandscapeView
is only called once a Notification is sent out telling the ViewController of an orientation change: the issue here is, that this is the method responsible for checking the orientation it self. (i.e. Starting the application this method is not called and therefore it doesn't check whether the device is in any other orientation)
The solution is quite simple: Force cal themethod at launch, i.e. in viewDidLoad . . .
[self updateLandscapeView]
This will force call the method and check the interfaces orientation, after the first time, the method will be called again when ever it receives a notification for a changed Orientation
Hope this helps someone Out There
It seems that the device assumes portrait unless you specify landscape only in settings. Your only option would be in your portrait view in the loadview method to detect the orientation and to swap views during launch.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
//Load vertical interface
}
else
{
//load landscape
}
}

iPad Orientation Checking UIView Class?

I have a UIView class which I add to my main UIViewController and I need to check the orientation of the device (iPad) at the launch of the app, in the viewDidLoad method. However because the class is a UIView (not UIViewController) I can't use methods such as willAnimateRotationToInterfaceOrientation.
So I attempted to use this in my UIView class:
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
However, testing with some breakpoints, whatever the orientation is, the of statement is never called, its skips right passed it. So what do you suggest I do to overcome this issue?
I need to somehow detect the orientation from the UIView class.
Thanks.
Where are you placing the check? The location could easily explain why it's not being called. To get rotation info, you could register for a notification, or have your view controller call a method in your view. Sample code for the latter:
// ViewController.m
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.customView willRotateToOrientation:toInterfaceOrientation];
}
// CustomView.m
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
// Handle rotation
}
The view controller method is one you override; the view's method should be declared in a header.
Update:
Alternatively, you can find out the rotation in the controller's `viewWillAppear':
// ViewController.m
- (void)viewWillAppear {
[self.customView willRotateToOrientation:[[UIDevice currentDevice] orientation];
}
The method in your view will be called accordingly.
One thing you can to is to register for orientation notification from NSNotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
...
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// do things
}
This is however suboptimal since iPad may be laying flat on the table when app starts, and you'll get UIDeviceOrientationUnknown then. Been here, done that...
I ended up doing a trivial check like this:
BOOL landscape = self.bounds.size.width > self.bounds.size.height;
if (landscape)
// landscape stuff
else
// portrait stuff
But in my case the view changed aspect ratio upon rotation. If this is your case too, it should work fine.

iPad app with multiple views, how do I make ONE single view viewable in landscape only?

After a supportive answer of miamk, I am a step closer to achieving what I want.. But there's the following issue now:
I have an iPad application that can be used in all four view modes (portrait up/down and landscape left/right). But at a certain point I have a View that I only want to be seen in landscape mode. So I do the following in the UIViewController that will trigger the action to view the landscape-only view:
- (void) showProperty:(Property *) property {
if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:#"PropertyViewController" bundle:[NSBundle mainBundle]];
propertyView.property = property;
[self.navigationController pushViewController:propertyView animated:YES];
[propertyView release];
propertyView = nil;
}
else {
RotateDeviceViewController *rotateView = [[RotateDeviceViewController alloc] initWithNibName:#"TabRotate" bundle: [NSBundle mainBundle]];
rotateView.property = property;
[self.navigationController pushViewController:rotateView animated:YES];
[rotateView release];
rotateView = nil;
}
}
This works fine and thus shows either the desired screen (PropertyViewController) when the iPad is held in landscape mode, and if not it shows the RotateDeviceViewController which shows the user a message that he/she is supposed to rotate the device to correctly view the screen.
So that works!
Then the problem arises in this RotateDeviceViewController.. There I have the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
[self showProperty];
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
- (void) showProperty {
PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:#"PropertyViewController" bundle:[NSBundle mainBundle]];
propertyView.property = property;
[self.navigationController pushViewController:propertyView animated:YES];
[propertyView release];
}
So as soon as I rotate the device (when viewing the RotateDeviceViewController) to landscape mode I show the user the PropertyViewController. This works... But when the PropertyViewController appears it shows my layout 90 degrees rotated. So basically it shows the content in portrait mode instead of using the landscape mode (which is actually the way you are holding the device)..
I hope this makes sense and someone can show me what's causing this.
A more elegant workaround (at least in terms of design) to #MacN00b's answer, is to set up a portrait view with a message that tells the user that he should rotate the device and only when he rotates it you show the view built for landscape.
Honestly, I think its ugly to have everything already rotated when the user is still in portrait orientation.
You can listen for orientation changes using:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
And then respond to those by loading the appropriate view...
- (void) orientationChanged:(id)object
{
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
}
If this is going into a dedicate UIView subclass for the screen in question you can make the portraitView contain a label notifying the user to rotate the screen to view the content and then make the landscape view contain your actual content.
I currently do this in an app and both views are contained in a single nib. Just be sure you set the orientation on the view properties in IB appropriately for each view...
Try this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Hope this helps.