我正在开发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
}
我为所有View控件使用以下子类:
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"))
}
}
提前致谢!
答案 0 :(得分:5)
我能够通过继承SLComposeViewController
import Social
import UIKit
class ComposeViewController: SLComposeViewController {
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
}
答案 1 :(得分: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应该是微不足道的。请注意,在假设这些视图控制器总是以模态方式呈现的情况下,您必须强制解包presentViewController。