Landscape orientation for one view controller ios - ios

I want to show one view controller in both orientation in iPhone. And when the screen is opening the preferred orientation should be portrait only. Then according to the device orientation it'll rotate. I found some solution and the screen is rotating too. But when I'm entering the screen it is auto rotating to landscape and than I've to manually rotate it to portrait.
Is there a simple solution to this problem?

You should implement preferredInterfaceOrientationForPresentation and
supportedInterfaceOrientations like:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIDeviceOrientationPortrait;
}
//You don't actually need to implement this as UIInterfaceOrientationMaskAllButUpsideDown is the default for iPhone
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}

Related

Force UiInterfaceOrientation from Landscape to Portrait?

I know may be it's a duplicate question, but I tried many answers described here and I can't get it working from hours.
I'm working on an application for IOS 6 and IOS 7, just I need to move from my first viewController "A" which is on Landscape orientation, to a second viewController "B" which is on Portrait orientation.
I configured the project to enable all desired orientation, set the "appropriate" code, but still get the second view displayed vertically on a landscape orientation..
here is the code I set for first controller :
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape ;
}
Plz help.
If you use the UINavigationViewController methods(pushViewController:animated: and popViewControllerAnimated:), the views will inherit the previous view's orientation.
On the other hand, if you use presentModalViewController:animated: and dismissModalViewControllerAnimated: methods, everything works perfectly.
Also here is a sample project which is also changing orientation as required by you

shouldAutorotate is not locking the screen orientation

I ma trying to lock screen orientation to only landscape orientation when a certain image is still visible, then when the image is hidden, unlock all orientations (targeting iOS 6):
-(BOOL)shouldAutorotate{
if (self.splashImageView.hidden == NO) {
return UIInterfaceOrientationMaskPortrait;//gets called when image is visible
}else{
return UIInterfaceOrientationMaskAll;//gets called when image is hidden
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self shouldAutorotate];
}
As you may notice, shouldAutorotate is called properly but the screen is always supporting landscape orientation even when the image is still visible, is there something missing?
P.S: Please note I am trying to get that to work on a tabbar view controller (a UIViewController subclass).
In your appdelegate you have those two methods.but do you have setting like in your project settings -> go to summary tab and see if orientation is set to only landscape or all.Just try to set those.

Deactivate automatic screen rotation temporarily

For our iPad application, I would like to allow for auto rotation of the screen in landscape mode (not portrait) to support both possible orientations. However, there are portions of our application during which the user needs shake and move the iPad into directions and orientations, that will trigger the auto rotation feature but should not.
is it possible to de- and re-activate the autoorientation, such that the orientation will be locked when entering this section of the app and unlocked when exiting?
if the specific section means another view controller, YES it is possible
just add this line of code to the controller ..
// which orientation that this view controller support
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
// prevent rotation
- (BOOL)shouldAutorotate
{
return NO;
}
// after present this view controller, which orientation do you prefer ?
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

iOS Auto rotation of view at first

I am working on an ios app that must work in both landscape and portrait, except one view, that should be always in landscape. So, I have:
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
If I lunch my application in landscape mode, and get to this view and rotate the device (iPad), this method is called and return false, so the interface doesn't rotate, good stuff.
BUT if I am in portrait and get to this view, this method is called and return a false, however the orientation doesn't change. In this context, if I rotate the device to landscape, the method return a true and the interface rotate properly and if I tried to rotate again to portrait, this method return false and the interface remains in landscape.
How can I achieve that the first time I get to this view, if the device is in portrait, the interface change to landscape?
Also, I tried with
- (NSUInteger)supportedInterfaceOrientations
but it never get called.
Thanks in advance!
shouldAutorotateToInterfaceOrientation: is depreciated in iOS 6.0. Override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.
From the docs on preferredInterfaceOrientationForPresentation
If your view controller implements this method, then when presented,
its view is shown in the preferred orientation (although it can later
be rotated to another supported rotation). If you do not implement
this method, the system presents the view controller using the current
orientation of the status bar.

UIDevice orientation not working as expected

I am having a problem with device orientation. I have an iPhone app that has some views, all of them should not rotate except one. So I take a look inside Info.plist; I select two device orientations, portrait and lanscape, and in the views I dont want to rotate so I put this.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
but all the views rotate. Even with these lines in them. If change Info.plist to support no portrait. It works ok, just the view that I want to rotate, I put
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
And it does not work. I use iOS 6. Also tried
- (NSUInteger)supportedInterfaceOrientations
- (BOOL)shouldAutorotate
Try specifying exactly the orientations you would like the view to rotate to. For instance, if you want portrait only use this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
For rotating to all views, you can still use YES, although this will allow it to rotate to the upside down view which isn't always desired. To get both landscape orientations and right side up portrait, use this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

Resources