Different Orientation for Different Views? - iphone

I'm making an iOS application that has an interface in the portrait view.
However, when displaying web content I want the view to display in a landscape format, because I want the website test to display larger initially without the user needing to zoom.
Can I tell the program so present this view only in landscape?
Thank you.

looks like you want to force the orientation to landscape mode only..
heres my solution in MyViewController.h
add this code on top of MyViewController's #interface
//force orientation on device
#interface UIDevice (PrivateOrientation)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
then in the implementation file (MyViewController.m)
add this code inside viewWillAppear:
//change orientation of device
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
this will force the device orientation to landscape mode left (or right depending what you want) if you want to go back to portrait mode after leaving the viewcontroller add this code inside viewWillDisappear:
//change orientation of device
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
finally implement shouldAutorotateToInterfaceOrientation: to force the view into landscape mode left or right (or both)
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
hope this helps :3

Related

Force landscape mode at the beginning of app, but allow later changes in orientation

I am planning to allow user to rotate the devices however during launch, i want the app start from landscape mode. May i know how can i do that?
Here is my code now for the orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
You need to check the orientation of the status bar.
Please check the following links :-
Force iOS app to launch in landscape mode
Modal View Controller force Landscape orientation in iOS 6
Force Landscape Orientation on iOS 6 in Objective-C
In the Project settings/target settings (the blue icon towards the top of the navigation bar on the left, in xcode), there are settings for specifying the orientation. You'll see 4 images, each of which can be selected or deselected.
Select only the one that you want initially for your app, and then you can use the shouldAutorotate and supportedInterfaceOrientation methods to allow or disallow certain orientations.
Supported interface orientations are also specified in the plist. Makes sure to keep the initial orientation for the app at the top of that supoortedInterfaceOrientations list in the plist file.

How to make my interface orientation automatically rotate to portrait?

I'm trying to get one of my xib file to rotate to portrait as if it were the default in the first place.
I have made my app to support only landscape orientations. In the plist I have set "Initial interface orientation to Landscape (right home button)" because of majority of the app runs on landscape mode.
The code I place in my implementation files are :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
Now when changing to the view controller that requires the interface to be in portrait mode I have placed this code on the xib file's implementation file to make it go into portrait mode and support portrait mode alone. So that even if the device is lying in landscape mode it would still run the view in portrait mode.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortrait;
}
Although this does not seem to work. It just remains in landscape mode and squeezes the image view and other objects I have placed in the xib file. How do I get the xib to rotate to portrait mode? Thanks.
change your Xib in Landscape Mode only and then click on your project file then target then select only Landscape mode or Portrait Mode(left/right or both)
then go to this method
I am using Landscape Mode.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
My experience shows that setting UIInterfaceOrientation in the plist file does not have any effect. On the other hand you can force an orientation (for example landscape) at start up time by setting:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
// your stuff
{
You can use this method also later on to force the logical orientation of the device. Note: this is an "official" method of Apple as of this document

Force UIViewController orientation

I did some research but cannot seem to find the answer to have my rootViewController of my navigationController be correct on start up. My original question was here: launch orientation of iPad is incorrect for landscape orientation (upside down).
In my info.plist, I have it set to support both landscape orientations. If I change my rootViewController to be:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// return UIInterfaceOrientationIsLandscape(interfaceOrientation); // original that does not work
return YES;
}
then my app starts in the right orientation. However, I do not want to support portrait modes. I only want to support landscape modes. I thought I could force the orientation and prevent it from switching to portrait modes by doing something like this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
UIDeviceOrientation orientation = UIInterfaceOrientationLandscapeLeft;
[UIApplication sharedApplication].statusBarOrientation = orientation;
}
else {
}
But this does not prevent the app from being rotated to portrait mode. Is it possible to force the orientation? Is there something else I need to do in order for the startup orientation to be correct (landscape mode only)? Thanks!
I just created a sample project, set my Supported interface orientations (iPad) to Landscape Left and Landscape Right (in info.plist).
I then used:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
and it works fine. It's forced to landscape not matter how i rotate it.
Do you have any other view controllers visible that might be returning YES to all orientations? This could confuse it.

UIView always loads in portrait mode

This one has me ripping my hair out. My iPad application is setup as follows:
In my app delegate I have this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
in my info.plist I have:
Initial Interface Orientation = UIInterfaceOrientationLandscapeLeft
In my first view controller that is loaded from the app delegate I have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
In my second view controller I have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Finally the views in both nibs are set to landscape in Interface Builder. The application starts in landscape mode and there is a button that when pressed assigns the second view to the first view:
self.view = secondView.view;
the problem is that even though everything is in landscape the new view is always loaded in portrait mode? Please any help on this would be very very appreciated!
This is the bit you want to change. This is what sets the allowed initial orientations. Change it to suit your needs.
It's in the summary tab of the Target's settings.
Or. if you aren't using Xcode 4 yet - you can set the orientations in the Info.plist:
And so you can see all the keys:

How to get InterfaceOrientation from iPhone (not DeviceOrientation)?

I've created a modalviewcontroller and all the subviews are created by code. When I'm testing the app, I find a problem. Then main cause of the problem is that an app shouldn't support UpsideDown orientation, but devices may happen to be in that orientation.
If I:
Rotate the device to Portrait orientation, and then to UpsideDown mode and presentModalView, the subviews in modalviewcontroller should appear the same as Portrait orientation.
Rotate the device to Landscape orientation, and then to UpsideDown mode and presentModalView, the subviews should be treated differently.
The above situation tells me that I should create subviews in modalviewcontroller according to previous InterfaceOrientation.
The problem is: How to get the previous screen's InterfaceOrientation? Getting the device orientation won't do any help in this situation.
PS: I'm writing a lib, I may give my users the interface to send me the "toInterfaceOrientation" from -willRotateToInterfaceOrientation:duration: but are there any ideas about how to get the orientation in my code?
In any UIViewController you can access the property interfaceOrientation like this:
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
// do stuff
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}