Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When SLComposeViewController loads, it rotates to the device orientation even when I have fixed the Main UIViewController to Portrait

I'm developing a Universal app in iOS8.1.

I would like the app to always appear in Portrait Orientation.

I have forced it by adding the following code. This is working.

In 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
    }

I use the following subclass for all my View Controllers:

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)
    }

}

Then however, I added an SLComposeViewController to my app so users can post to Facebook. If the user opens this SLComposeViewController while their device is in landscape, the SLComposeViewController takes over and rotates the screen, including all other ViewControllers that are already presented.

I would like to force the SLComposeViewController to always stay in Portrait Orientation, but can't figure out how to get it to work.

Can anyone help me?

This is the code I use to open the 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"))

    }
}

Thanks in advance!

like image 514
Peter Hanke Avatar asked Mar 17 '23 19:03

Peter Hanke


1 Answers

I was able to solve this by subclassing SLComposeViewController

import Social
import UIKit

class ComposeViewController: SLComposeViewController {

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

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

}
like image 77
Nick Lee Avatar answered Apr 06 '23 13:04

Nick Lee