Reputation: 23
I'm new in Objective-C. I wanna fix portrait mode for specific view. Other view using landscape mode and put back portrait mode when finish the view. But, still landscape mode on specific view.
- (NSUInteger) supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
// Return the orientation you'd prefer - this is what it launches to. The
// user can still rotate. You don't have to implement this method, in which
// case it launches in the current orientation
return UIInterfaceOrientationPortrait; }
I add the code to specific view.
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
And then, this code input the method.
How can I modify to fix this?
Upvotes: 1
Views: 398
Reputation: 850
It's really very easy. Let's you have two ViewController class named 'ViewController' and 'SecondViewController'
First you have to import SecondViewController in 'AppDelegate' class.
Then copy and paste this two methods in your AppDelegate.m file
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController] isKindOfClass:[SecondViewController class]]) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskLandscape;
}
and
- (UIViewController *)visibleViewController:(UIViewController *)rootViewController {
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self visibleViewController:lastViewController];
}
if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
UIViewController *selectedViewController = tabBarController.selectedViewController;
return [self visibleViewController:selectedViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self visibleViewController:presentedViewController];
}
Now run and see.
Upvotes: 1