My app has Portrait mode only, as follow image: Device Orientation
But it needs to support Landscape mode in customized UIViewController. I have override follow functions:
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
It worked in most cases, but somehow it would acted like follow image: problem picture
There seems to have a conflict between the device orientation and MyViewController orientation.
PS: My device is iPhone 6s Plus, system is iOS 10.0, this problem also exist in iPhone 6s.
If you are wondering for ViewController
specific orientation then please refer below answer
https://stackoverflow.com/a/27010196/6325474
Try this .
AppDelegate.h
@property () BOOL restrictRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll; // you can change orientation for specific type . for ex landscape left
}
ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
viewDidLoad
[self restrictRotation:YES]; or NO
you need to write this line in every viewcontroller where you need to modify
for more : https://stackoverflow.com/a/25952571/3901620
You can use following code in AppDelegate .This is only possible solution which will work Swift code
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) ->
UIInterfaceOrientationMask {
let vc = self.window?.currentViewController()
if vc?.isKind(of: YourDesiredVCToChangeOrientation.self) == true {
return UIInterfaceOrientationMask.landscape;
} else {
return UIInterfaceOrientationMask.all ;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.