2015-09-04 70 views
2

启动屏幕当前默认为设备当前的方向。 我试图以编程方式设置它,但它仍然不起作用。Xcode - 强制启动屏幕在横向加载

override func viewDidLoad() { 
    super.viewDidLoad() 

    if UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait || UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown { 
     UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation") 
    } 
} 

enter image description here enter image description here

+0

设备的取向是不会被'viewDidLoad中()'注意到,因为在'viewDidLoad中()'运行的代码视图呈现之前。你编写的代码(或类似的东西)可以在'viewWillAppear()'中使用。我意识到这个问题很古老,但我认为我的评论可能对提问人员以外的人有用。 – ClayJ

回答

0

最后,我想出了一个解决方案。启动屏幕在应用程序运行之前生成,所以这里有一个技巧。

在您的项目的常规设置中,只检查横向左侧&右侧,因此它只会生成横向飞溅图像。和调酒的一个NSBundle的infoDictionary方法如下:

@interface NSBundle (Hook) 
@end 

@implementation NSBundle (Hook) 

+ (void)load { 
    [NSBundle jr_swizzleMethod:@selector(infoDictionary) 
        withMethod:@selector(hook_infoDictionary) 
         error:NULL]; 
} 

- (NSDictionary<NSString *, id> *)hook_infoDictionary 
{ 
    NSMutableDictionary <NSString *, id> *dict = [[self hook_infoDictionary] mutableCopy]; 
    dict[@"UISupportedInterfaceOrientations"] = @[@"UIInterfaceOrientationPortrait", 
                @"UIInterfaceOrientationLandscapeLeft", 
                @"UIInterfaceOrientationLandscapeRight", 
                @"UIInterfaceOrientationPortraitUpsideDown", 
                ]; 
    return [dict copy]; 
} 

@end