iOS装置:特殊风景模式
问题描述:
我的应用程序只有人像模式,如下图: Device OrientationiOS装置:特殊风景模式
但它需要支持横向模式自定义的UIViewController。我有如下重写功能:
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
它的工作在大多数情况下,但不知何故,它会表现得像如下形象: problem picture
似乎有设备的方向和MyViewController取向之间的冲突。
PS:我的设备是iPhone 6s Plus,系统是iOS 10.0,这个问题也存在于iPhone 6s中。
答
试试这个。
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
你需要写这条线在每一个地方需要修改
更多的ViewController:https://*.com/a/25952571/3901620
答
可以在AppDelegate中。这使用下面的代码是唯一可行的解决方案,将工作 斯威夫特代码
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 ;
}
}
我可以建议你做到这一点的替代方案吗? – KKRocks
是的,PLZ,我想知道,thx – zxbeef
检查我的答案。它在我的项目中运行良好。 – KKRocks