MPVolumeView路由列表支持所有方向并忽略底层视图控制器

时间:2015-12-18 01:47:04

标签: ios orientation screen-rotation mpvolumeview

这似乎是操作系统错误,但仍需要解决方法。

使用基于视图控制器的方向定位iOS 8或9,MPVolumeView的路由列表将始终旋转,即使其父视图控制器仅支持单个方向。

这可能导致系统进入错误的方向状态,其中视图控制器以纵向显示,但状态栏(和控制中心)是横向的。

创建了一个演示错误的测试项目:https://github.com/NextFaze/MPVolumeViewTest

1 个答案:

答案 0 :(得分:1)

使用调配的hacky解决方案:

#import "UIViewController+RoutingSheet.h"
#import <objc/runtime.h>

@implementation UIViewController (RoutingSheet)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originalSelector = @selector(shouldAutorotate);
        SEL swizzledSelector = @selector(shouldAutoRotateOverrideRoutingSheet);
        Method originalMethod = class_getInstanceMethod(self, originalSelector);
        Method extendedMethod = class_getInstanceMethod(self, swizzledSelector);
        method_exchangeImplementations(originalMethod, extendedMethod);
    });
}

- (UIWindow *)currentWindow {
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (window.rootViewController == self)
            return window;
    }
    return nil;
}

- (BOOL)shouldAutoRotateOverrideRoutingSheet {
    UIWindow *window = [self currentWindow];
    if (window != nil) {
        NSString *className = NSStringFromClass(window.class);
        if ([className containsString:@"MPAVRoutingSheetSecureWindow"]) {
            return NO;
        }
    }
    return [self shouldAutoRotateOverrideRoutingSheet];
}

@end