iOS6.0 Pushing new viewController in portrait orientation from landscape viewController

冷暖自知 提交于 2019-12-13 04:42:22

问题


My application is navigation based and mainly runs in portrait orientation, But one view controller supports both portrait and landscape orientation. Now the problem is, When I push the new viewcontroller from landscape view controller, the new view controller is also pushed in landscape mode though i want it in portrait mode.

Also when I pop view controller from landscape controller then the poped view controller is getting displayed in portrait mode.

I don't know what is wrong with my code.

Here is the my code snippet and information used for these orientation support.

In info.plist file I kept support for all orientation but portrait upsidedown.

I have also added category for navigation controller category as below.

@implementation UINavigationController(Rotation_IOS6)
-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

I have also created a UIViewController subclass that acts as superclass for all classes. Here are the orientation methods for super class.

@implementation ParentViewController

- (BOOL)shouldAutorotate{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}
@end

And the orientation methods controller that supports landscape are as below.

@implementation LandscapeController
#pragma mark -
#pragma mark Orientation Methods
- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end

Thanks in advance.


回答1:


Here is the method to push the view.take an object switch

At switch.m

+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease
{

VControllerToLoad.navigationController.navigationBar.frame=CGRectMake(0, 0, 568, 44);
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect windowFrame = [[UIScreen mainScreen] bounds];
CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
VControllerToLoad.view.frame = firstViewFrame;

//check version and go

if (IOS_OLDER_THAN_6)
{
    [((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:VControllerToLoad.view];
}
else
{
    [((AppDelegate*)[UIApplication sharedApplication].delegate).window setRootViewController:VControllerToLoad];

}
  [VControllerToRelease.view removeFromSuperview];
}

You can push to portrait view from landscape mode like this using above method

 PortraitViewController *portraitVC=[[PortraitViewController alloc]initWithNibName:@"PortraitViewController" bundle:nil];

  [Switch loadController:portraitVC andRelease:self];

At landscape view controller.m

    - (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.title=@"Landscape";
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.navigationController.navigationBarHidden = NO;
    if (IOS_OLDER_THAN_6)
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    // Do any additional setup after loading the view from its nib.
}

#ifdef IOS_NEWER_OR_EQUAL_TO_6

-(BOOL)shouldAutorotate  
{
    return YES; 
 }

- (NSUInteger)supportedInterfaceOrientations    
 {
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
 }

#endif



回答2:


I have had a similar problem. In my case this very one view controller had to be displayed in landscape all times and on the way back the other view controllers must be set back to portrait.

I found the following solution in the FAQ: iPhone Landscape FAQ and Solutions

The key point of that idea is that the device can be forced into a certain orientation only by one way: 1. Set the orientation of the staus bar. 2. Present any view controller modally (!) that supports the targeted orientation.

Well, you do not want to present anything modally? Do not worry. Nothing stops you from ... 3. instantly dismissing that very view controller once it was presented. It will not even be visible to the user when you do it that way. But once that is done, your device remains in this very orientation. 4. Push the view controller that you want to display or even segue to it in the event that you use storyboards.

To me that seemed to be the only workable solution. In addition to that answer in the FAQ everything else must be set properly too. The supported device orientations for the full project must be basically all. The supported orientations for each view controlelr must be those, which each view conroller supports and shouldAutoRotate should return YES. It gets more difficult if a tab bar controller is involved. Get back to me if your app is based on a tab bar.



来源:https://stackoverflow.com/questions/14896607/ios6-0-pushing-new-viewcontroller-in-portrait-orientation-from-landscape-viewcon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!