LazyBlog

當專案的橫豎設定為時(如圖)

想讓某個畫面可以旋轉(通常會在影音App出現此需求)

第一步

AppDelegate.m

1
2
3
4
5
6
7
8
9
10
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}

return orientations;
}

第二步

你要可以旋轉的ViewController.m

1
2
3
-(BOOL)shouldAutorotate{
return YES;
}
1
2
3
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return (UIInterfaceOrientationMaskAll);//依據你的需求更換,這邊設定為全部方向允許
}

補充-強制旋轉

1
2
3
4
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
}

 評論