2015-09-04 75 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