iOS force landscape in one viewController

2019-06-03 15:32发布

问题:

Sir,

I am working on the mapview module which landscape is the only orientation allowed but others for portrait only. When it comes to running on device ios 7 and 8 , the view controller is still presented as portrait orientation unless I have to manually turn the device to landscape . Would you please tell me what other steps to do ?

The below is my code

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window; 
@property (nonatomic)  BOOL isTaskPoint;

@end

AppDelegate.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) return YES;
    return NO;
}

PreviousController.m

MapViewController * sliderVC = [[MapViewController alloc] init ];
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;

appDelegate.isTaskPoint = TRUE;
sliderVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:sliderVC animated:NO completion:nil];
sliderVC.view.backgroundColor =  [UIColor clearColor];

//   MapSwift maps =

MapViewController.h
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
    AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;

    appDelegate.isTaskPoint = FALSE;
    [self dismissViewControllerAnimated: NO completion:nil];
}

MapViewController

- (BOOL) shouldAutorotate {
    return YES;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation ==  UIInterfaceOrientationLandscapeRight|| interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

回答1:

There is one trick that really works. You can access the status bar and set its orientation. That becomes active next time a view is displayed modally. However, right after displaying modally you can remove the modally displayed veiw controller. When you do that within the same method, then the user would not noticing anyhing. Now the device has the desired orientation. You can now safely push the view controller that you want to be in another orientation.

Do not forget to rotate it back, when returning from that view controller!

See the answer to this question. It comes with some code sniplets. Force controllers to Change their orientation in Either Portrait or Landscape



回答2:

If you want to disable or enable specific orientation in some view controller then this might be helpful to you.

And if you want to open some view in specific orientation then use this in viewDidLoad

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];