【问题标题】:When SLComposeViewController loads, it rotates to the device orientation even when I have fixed the Main UIViewController to Portrait当 SLComposeViewController 加载时,即使我已将 Main UIViewController 固定为 Portrait,它也会旋转到设备方向
【发布时间】:2015-03-12 00:31:18
【问题描述】:

我正在 iOS8.1 中开发通用应用程序。

我希望应用始终以纵向显示。

我通过添加以下代码来强制它。这是有效的。

在 AppDelegate 中:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        let value = UIInterfaceOrientation.Portrait.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")

        return true
    }

我的所有视图控制器都使用以下子类:

class PortraitViewController: UIViewController {

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }

}

然后,我在我的应用程序中添加了一个 SLComposeViewController,以便用户可以发布到 Facebook。如果用户在他们的设备处于横向状态时打开此 SLComposeViewController,则 SLComposeViewController 会接管并旋转屏幕,包括已呈现的所有其他 ViewController。

我想强制 SLComposeViewController 始终保持纵向,但不知道如何让它工作。

谁能帮帮我?

这是我用来打开 SLComposeViewController 的代码。

func pressFacebook(){
    if PortraitSLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
        var facebookSheet: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

        facebookSheet.setInitialText("My Status!")
        self.presentViewController(facebookSheet, animated: true, completion: nil)
        facebookSheet.addURL(NSURL(string: "www.blahblah.com"))
        facebookSheet.addImage(UIImage(named: "fb.jpg"))

    }
}

提前致谢!

【问题讨论】:

    标签: ios iphone swift slcomposeviewcontroller


    【解决方案1】:

    我可以通过继承SLComposeViewController来解决这个问题

    import Social
    import UIKit
    
    class ComposeViewController: SLComposeViewController {
    
        override func shouldAutorotate() -> Bool {
            return true
        }
    
        override func supportedInterfaceOrientations() -> Int {
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
        }
    
    }
    

    【讨论】:

    • 我也有同样的问题,但是你怎么知道你在 Objective-C 中的代码?
    • @joeygusto 我不确定你在问什么。
    【解决方案2】:

    您可以继承 SLComposeViewController 或简单地扩展它。

    以下是我以可概括的方式解决此问题的方法。 UIActivityViewController 和它呈现的 SLComposeViewController 都使用以下类别扩展检查它们呈现的视图控制器以确定它们的自动旋转配置:

    @implementation UIActivityViewController (FixAutoRotation)
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.presentingViewController.preferredInterfaceOrientationForPresentation;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return self.presentingViewController.supportedInterfaceOrientations;
    }
    
    - (BOOL)shouldAutorotate {
        return false;
    }
    
    @end
    
    
    @implementation SLComposeViewController (FixAutoRotation)
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.presentingViewController.preferredInterfaceOrientationForPresentation;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return self.presentingViewController.supportedInterfaceOrientations;
    }
    
    - (BOOL)shouldAutorotate {
        return false;
    }
    
    @end
    

    此代码在 Objective-C 中,但更改为 Swift 应该很简单。请注意,假设这些视图控制器始终以模态方式呈现,您将不得不强制解包presentingViewController。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      相关资源
      最近更新 更多