简体   繁体   English

在iOS 8中启动应用程序时,如果设备处于横向,则如何强制纵向显示

[英]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.

相关问题 iOS 6迫使设备朝向横向 - IOS 6 force device orientation to landscape 设备更改方向时如何立即检测UIInterfaceOrientation方向(纵向,横向) - How to detect UIInterfaceOrientation immediately when device change Orientation (Portrait,Landscape) 即使装置的人像方向锁定处于开启状态,也强制旋转iOS - Force Rotate iOS Even When Device's Portrait Orientation Lock is ON iOS为纵向时返回横向 - iOS return landscape orientation when is Portrait iOS 8设备方向 - ios 8 device orientation landscape 当设备的方向为横向时,如何在应用程序中的任何位置加载特定视图,并在纵向时切换为横向视图? - How to load a particular view when device's orientation is landscape any where in the app and switch to the lat view when portrait? 强制控制器以纵向或横向更改其方向 - Force controllers to Change their orientation in Either Portrait or Landscape 在ipad中从横向视图呈现模态viewcontroller时如何强制从纵向显示viewcontroller的方向 - how to force the orientation of viewcontroller to be displayed from portrait when presenting modal viewcontroller from landscape view in ipad 当方向锁定打开且应用程序仅支持纵向时,如何检测设备方向 - How can I detect device orientation when the Orientation Lock is on and the app only supports Portrait iOS-iPad Air和iPad Pro仅在横向模式下强制设备定向 - iOS - iPad Air and iPad Pro force device orientation in landscape only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM