UIView always loads in portrait mode - iphone

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:

Related

Forcefully load landscape view when user in Portrait view

In my Application I'm using a navigation controller to mange views. My login page support both portrait and landscape views. When user logged in my second view is home and it support only landscape mode. What I want to do is when user login to the home using portrait view home page should appear in landscape view even though the device in portrait.
So what I did was I change the status bar orientation in to landscape int the home page's viewWillAppear method as follows;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:NO];
UIDeviceOrientation orien = [[UIDevice currentDevice] orientation];
}
also I have override the shouldAutorotateToInterfaceOrientation as follows
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
My problem is even the status bar changed to landscape my UIViewController (home) is remains in landscape mode. When i'm debugging I found that even I change the status bar orientation to landscape,[[UIDevice currentDevice] orientation] returns portrait. I went through internet whole day. And implement lot of solutions proviede by other but my whole day wasted. can some one guide me to solve these issue.
you just need to like:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
else
{
return NO;
}
}
for particular class you want to open landscape Only
in ios6:-
-
(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Apple does not want you to force the orientation of the device. There is a trick though.
Unfortunately I do not have access to my code.
1. Your app in general supports all orientations.
2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations).
3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations.
That is fine. But it would still require the user to actually rotate the device. Otherwise the whole orientation change mechanism would not be invoked.
Now, when you want to force the orientation change, do the following:
4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed.
That alone would not do anything. Plus it would not take any effect if the next view controller is pushed. It would work fine only when the next view controller is presented modally.
5a. So if you want to present the rotated view controller modally, then do it.
5b. If you still need to push it then:
5b1. Create an empty UIViewController instance. alloc/init will do.
5b2. Present it modally
5b3. Dismiss it modally
Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now.
5c4. Next push the view controller that you want to display roated.
And vice versa on your way back :)
All the above gets more complicated when you use a tab bar. Do you use a tab bar?
I managed to get that working with a tab bar which I had to subclass to overwrite its rotation methods. In an app without tab bar I subclassed UIApplication (!) but don't rembember wether that was really required or wether I did that out of convenience (instead of aplying the changes to 50+ view controllers). But in principle the above is it that does the trick.
PS: You find a more detailled answer here along with code samples:
Presenting Navigation Controller in Landscape mode is not working ios 6.0
You can try with
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
and
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
or try presenting it as a Modal, rather than pushing it.

shouldAutoRotate not getting called [duplicate]

I've been writing my Universal application in portrait mode,
and now after about 15 nib files, many many viewCotnrollers,
I'd like to implement the shouldAutorotateToInterfaceOrientation and design some screens in Landscape mode.
adding :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
to ALL of my viewControllers, does not do the work.
During Debug, i see that this method is called, but it just won't work! not in the simulator, not in the device, not in Iphone, not in Ipad!
i've searched some answers in the forum, and saw some advises to use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown );
}
Didn't worked either,
adding:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
and
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
to my viewDidLoad and viewDidUnload respectively didn't worked either.
I'm lost.. Any help will do!
just one more info... all my Views are of type UIControl, as i needed the TuchUpInside to work.
Appriciate your help.
Make sure all of your parent views have autoresizesSubviews = YES. You may need to do this in code if you haven't set springs and struts in IB for all of your views.
Quoting the Interface Builder User's Guide:
Important: In a Cocoa nib file, if you
do not set any springs or struts for
your view in Interface Builder but
then do use the setAutoresizingMask:
method to add autosizing behavior at
runtime, your view may still not
exhibit the correct autoresizing
behavior. The reason is that Interface
Builder disables autosizing of a
parent view’s children altogether if
those children have no springs and
struts set. To enable the autosizing
behavior again, you must pass YES to
the setAutoresizesSubviews: method of
the parent view. Upon doing that, the
child views should autosize correctly.
A couple other things to be aware of:
A UINavigationController will only autorotate if its root view controller is also set to autorotate.
A UITabBarController will only autorotate if all of its view controllers are set to autorotate.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
UIInterfaceOrientation des=self.interfaceOrientation;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
{
if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portairait
{
}
else//ipad -landscape
{
}
}
else//iphone
{
UIInterfaceOrientation des=self.interfaceOrientation;
if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
{
}
else //iphone -landscape
{
}
}
return YES;
}
Which iOS are you building for? It was deprecated in iOS 6.0. (You should override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.)
Also you can call shouldAutorotate on the UIViewController class:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/shouldAutorotate
Ensure you have checked the Supported Interface Orientations within the "Summery" tab of the project settings (Select the project name in the 'Project Navigator' at the very top).
If you have not selected the orientations you want to use here, then the simulator/iphone will not allow the screen to change orientation.
I had this problem but it worked in iOS6 but not iOS5. Turns out I had a view in my storyboard that I hadn't made a viewcontroller class for yet.
...and last but not least, make sure you haven't activated the setting "Portrait Orientation Locked" on your test device (of course doesn't apply to the simulator), this will disable rotating in any app no matter what shouldAutorotateToInterfaceOrientation returns.

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

Locking the orientation in an iPad app (plist or UIViewController?)

I'd like the app to work like it would be as if I locked the orientation manually. I'm trying to find how I can lock the orientation for an app. In the info.plist, I have this setting:
Supported interface orientations (iPad)
Item 0 Landscape (right home button)
Item 1 Landscape (left home button)
I thought that would be enough to keep my viewControllers from staying in landscape mode and not portrait. But it does not. Do I need to do
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
in ALL my viewControllers? Thanks!
All though implementing shouldAutorotateToInterfaceOrientation in all your view controllers will work, it is probably not the fastest or most practical way of doing what you are trying to accomplish.
If any of your view controllers in your hierarchy do not conform to the orientation change, then iOS will stop trying to rotate them. What this means is that only your root view controller needs to have implemented shouldAutorotateToInterfaceOrientation with only landscape orientations. Each view controller pushed or added will conform to that function.
I have had to do this in several of my apps and it was required for several reasons.
In the end and after a lot of testing, we determined that the condition has to be set on the info.plist AND on every viewController.
So make sure it is set on the plist and that every shouldAutorotateToInterfaceOrientationonly returns yes for the allowed orientation.
This is because the plist will help you with allowed LAUNCH orientations, but your app could still rotate afterwards, specially when using modal views.
You can download one of my free apps that does thins on iPad: http://itunes.apple.com/mx/app/hoteles-city/id471505865?mt=8
Yes you do.
I have a different solution however. In every UIViewController, I use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
return NO;
}
}

Different Orientation for Different Views?

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