在iOS 8中隐藏音量HUD



在iOS 8之前的版本中,有一个隐藏系统卷覆盖的简单技巧。您只需创建一个MPVolumeView并将其嵌入到视图层次结构中的某个位置即可。此处对此进行了记录https://stackoverflow.com/a/7888977/3943258以及在许多其他堆栈溢出响应中。

然而,我发现这个技巧在iOS8中似乎不起作用。我正在拔头发,想办法解决它。有人知道在iOS 8中是否有办法做到这一点吗?

值得注意的是,当我试图隐藏HUD(音量按钮充当相机上的快门)时,我正在使用的应用程序有一个活动的AVCaptureSession。不确定是否会有一些副作用。

好吧,这滥用了一个私有的API,但我发现它有效。

- (void)setVolumeHidden:(BOOL)hidden
{
    NSString *str1 = @"etSystemV";
    NSString *str2 = @"eHUDEnabled";
    NSString *selectorString = [NSString stringWithFormat:@"s%@olum%@:forAudioCategory:", str1, str2];
    SEL selector = NSSelectorFromString(selectorString);
    if ([[UIApplication sharedApplication] respondsToSelector:selector]) {
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIApplication instanceMethodSignatureForSelector:selector]];
        invocation.selector = selector;
        invocation.target = [UIApplication sharedApplication];
        BOOL value = !hidden;
        [invocation setArgument:&value atIndex:2];
        __unsafe_unretained NSString *category = @"Ringtone";
        [invocation setArgument:&category atIndex:3];
        [invocation invoke];
    }
}

使用MPVolumeView可以在iOS 9中工作。

let systemVolumeView = MPVolumeView(frame: CGRectMake(-500, -100, 0, 0))
view.addSubview(systemVolumeView)

简化了一点解决方案:

#import <UAObfuscatedString/UAObfuscatedString.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL selector = NSSelectorFromString(NSMutableString.string.s.e.t.S.y.s.t.e.m.V.o.l.u.m.e.H.U.D.E.n.a.b.l.e.d.colon.f.o.r.A.u.d.i.o.C.a.t.e.g.o.r.y.colon);
if ([[UIApplication sharedApplication] respondsToSelector:selector]) {
    [[UIApplication sharedApplication] performSelector:selector withObject:@NO withObject:@"Ringtone"];
}
#pragma clang diagnostic pop

最新更新