my app contains two table view controllers. in the first one i want the view to be able to be rotated left and right (in addition to the portrait mode), however in the second table view controller ( which i navigate to after tapping a cell from the first table) i want it to only be viewed in portrait mode. i tried this code but it didn't work, it keeps on rotating.
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL) shouldAutorotate {
return NO;
}
note: i did actually enabled the left/right/portrait orientation from the Summary tab of the project target. any fix?
Create a category for UINavigationController which include the following methods:
(Works both for iOS 6 and iOS 5)
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
Then implement these methods in your controllers
First:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if (RUNNING_IPAD) {
return UIInterfaceOrientationMaskAll;
}
else {
return UIInterfaceOrientationMaskAllButUpsideDown;
};
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (RUNNING_IPAD) {
return YES;
}
else {
return toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown;
}
}
And Second:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return NO;
}
The rotation settings of the project should look like:
Actually i found a solution for my problem:
-(BOOL)shouldAutorotate{
return YES;
}
-(NSInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
this will restrict a view to portrait even if you toggled the left/right orientations in the Summary tab of the project's Traget.
Related
I try to force navigationController to be in orientation Portrait on my application. But I solved the problem by subclassing the navigationController class by this way :
- (BOOL)shouldAutorotate
{
if (self.visibleViewController && [self.visibleViewController respondsToSelector:#selector(shouldAutorotate)])
{
return [self.visibleViewController shouldAutorotate];
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.visibleViewController && [self.visibleViewController respondsToSelector:#selector(supportedInterfaceOrientations)])
{
return [self.visibleViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}
So all my controller inherits to this. But my problem is when I start my application with my iPhone in landscape mode, my viewController not respect the interfaceorientationlandscape. But when I turn my iPhone it's works and not change if the orientation changes.
I search how can I force my orientation to change if is not in portrait in viewDidLoad or if possible in viewDidAppear.
If you want to have this app-wide
You can get there by clicking on the corresponding menu item:
Otherwise if you want to set this for one ViewController only
- (BOOL)shouldAutorotate {
return YES; // or NO
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft; // or UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationPortrait
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape; // or UIInterfaceOrientationMaskPortrait
}
I am trying to lock one of the views in my app to LandscapeRight. The code I used is shown below.
// To landscape
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
However, it still rotates.
Do I miss something?
I reply to this question here:
Disabling rotation for current screen?
you should use:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//Choose your available orientation, you can also support more tipe using the symbol |
//e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
return (UIInterfaceOrientationMaskLandscapeRight);
}
If you return NO, your viewController doesn't enter in the method supportedInterfaceOrientations.
I want to support the autorotation in iOS 6 and 7. In my project I have a UITabBar with 4 ViewControllers. Only one ViewController of them should support the autorotaion to Portrait and Landscape. The other views should support only the Portrait style.
I hope you have an idea how to implement this function. shouldautorotatetointerfaceorientation doesn't work in iOS 7 :(
I added a UITabBarConntroller to control the autorotation
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if (self.selectedIndex == 1)
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationPortrait);
else
return UIInterfaceOrientationMaskPortrait;
}
Maybe there is a way to change the Orientation manual when the View with the index 1 did disappear??
I hope you have a solution!
Set the orientations settings like this:
In UITabBarController:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
-(NSUInteger)supportedInterfaceOrientations
{
if (self.selectedViewController)
return [self.selectedViewController supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
return YES;
}
Open project
Go to the General tab
Under orientation options check all checkbox orientation that you need to support
Go to the implementation of first view controller and add this code:
- (BOOL)shouldAutorotate
{
return YES;
}
Also for supporting iOS 6 implement method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationMaskAll);
}
Go to all other view controllers that are represented your tabs and set:
- (BOOL)shouldAutorotate
{
return NO;
}
Also for supporting iOS 6 implement method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Check this link as well
I have a problem with orientations in my app. Assume that I have two views (with dedicated view controllers):
first should be displayed in portrait (it is displayed correctly)
second should be displayed in landscape (it is not displayed correctly)
It is coarctated and displayed in portrait (like in second image below).
When I rotate device horizontal and back to portrait everything is OK. But after pushing view it displays incorrectly (images below). How can I fix this?
I use CustomNavigationController whish inherits from UINavigatorControler and implements three methods:
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}
In application delegate I initializing controller in this way:
self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];
First view controller implements orientation functions in this way:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait)
return YES;
return NO;
}
Second view controller implements orientation functions in this way:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
hi declare a global variable BOOL isLandScape ;
initialize it as isLandScape=NO;
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ((orientation == UIInterfaceOrientationLandscapeRight)||(orientation == UIInterfaceOrientationLandscapeLeft))
{
isLandScape=YES;
return YES;
}
else
{
isLandScape=NO;
return NO;
}
yourObject.frame=CGRectMake(isLandScape?0:0,isLandScape?0:0,isLandScape?1024:768,isLandScape?768:1024);
}
Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need.
Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). Add the following method in this custom navigation controller (this if for landscape orientation but you can change to portrait too).
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
and add to your view controller that should rotate:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. Then, if you want to block some orientations for a particular window:
– application:supportedInterfaceOrientationsForWindow:
To do this You can use this function:
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
You can use this wherever you want, but in application delegate (in didFinishLaunchingWithOptions) i must put this code:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
It's works perfectly!
I have two UIViewControllers in my UINavigationController stack, in which i need to support portrait and landscape orientations for the first viewController alone and for the second viewController, i need portrait support alone and not landscape.
In ViewController1, I have implemented the following methods,
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self changeFrames:toInterfaceOrientation]; //changing frames for orientation accordingly
}
and in ViewController2:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Also in project target, i have set to support all the 4 orientations.
Looking for any kind of help to solve the above issue.
Thanks in Advance.