[英]How to force portrait orientation if device is in landscape orientation when the app launches in iOS 8
My application allows all four orientations, but the rootviewcontroller should only allow portrait orientation. 我的应用程序允许所有四个方向,但是rootviewcontroller应该只允许纵向。
I override the supportedInterfaceOrientations method in my rootviewcontroller, but if the device is held in landscape orientation when the app launches then the view controller displayed incorrectly in landscape orientation even though only portrait orientation is allowed. 我在rootviewcontroller中重写了supportedInterfaceOrientations方法,但是如果在应用启动时将设备保持在横向,则即使仅允许纵向,视图控制器也无法正确显示横向。 This is an iOS 8 specific issue.
这是iOS 8的特定问题。
override func supportedInterfaceOrientations() -> Int {
return UIInterfaceOrientation.Portrait.rawValue
}
In ViewController: 在ViewController中:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Swift version: 迅捷版:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
For me implementing AppDelegate function application supportedInterfaceOrientationsForWindow did the trick 对我来说,实现AppDelegate函数应用程序supportedInterfaceOrientationsForWindow可以解决问题
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
if let viewController = self.window?.rootViewController?.presentedViewController as? PortraitViewController{
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}else{
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
where PortraitViewController should be replaced with the name of your root view controller class 应该用您的根视图控制器类的名称替换PortraitViewController的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.