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

MPVolumeView route list is supporting all orientations and ignoring underlying view controller

这似乎是一个 OS 错误,但仍然需要解决方法。

针对iOS8或9,使用基于视图控制器的方向,MPVolumeView的路由列表将始终旋转,即使其父视图控制器仅支持单一方向。

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

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

使用 swizzling 的 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