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;
}
Related
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;
}
Is it possible to support all orientations yet once the view is loaded in the launched orientation disallow the user from then rotating orientation? So say we have a loading screen which is launched in portrait, we want to disable any rotation on this screen. Same goes for if the app is launched in landscape, then we want to disallow rotation to portrait.
you need to subclass 'UINavigationController', implement shouldAutorotate and use your navigation controller class in your storyboard.
override the shouldAutorotate method on the initial view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.
- (BOOL)shouldAutorotate{
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[DetailViewController class]])
return NO;
return YES;}
good luck
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.
my app has been working fine until I've tried making some changes using the ios 6 SDK
The app runs in portrait mode 99% of the time. This has been constrained by only allowing portait mode to be available in the info.plist
There is one view controller which needs to be shown in landscape mode. This is achieved "manually" by simply rotating the view by 90 degrees, like so:
self.view.transform = CGAffineTransformMakeRotation(3.14159/2);
This still works fine in iOS 6.
However, this view controller has some text fields. When the user taps one it shows the keyboard. Since I've only rotated the view (and not actually changed the orientation of the device), the keyboard comes out in portrait mode, which is no good.
In previous versions of iOS, I set the orientation of the status bar to be landscape, and as a byproduct, this would set the keyboard to be landscape as well, like this:
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
However, this has stopped working for iOS 6.
I have read a million stack overflows trying to get this to work, but still have no luck.
Changing the keyboard orientation and transform is an difficult part and not a good solution(especially when it changes status bar orientations).
Better solutions is to allow application to support all orientations.
Implement the Orientation delegates inside your ViewControllers asper the rotation support.
For Supporting only Landscape
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
For Supporting only Portrait
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
In my App, I have a tabbarController and 5 view controllers managed by it.
I only want one UIView rotate to landscape and make the tabbar invisible when rotate to landscape. In current condition, all my views autorotate themselves which is not what I expect.
The code of UIView which I dont want it rotate is:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationLandscapeLeft) {
// stop the rotation, keep it as portrait orientation
// from app document, it says for UITabbarController, rootview which is uitabbarcontroller and views managed by it should all agree on the same orientation otherwise they wont rotate.
// how can i do this?
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}