在iOS 8中隐藏卷HUD

时间:2014-10-10 14:52:05

标签: ios camera ios8 avcapturesession mpvolumeview

在iOS 8之前的版本中,有一个隐藏系统卷覆盖的简单技巧。您只需创建一个MPVolumeView并将其嵌入您的视图层次结构中。这里记录了https://stackoverflow.com/a/7888977/3943258以及许多其他Stack Overflow响应。

然而,我发现这个技巧似乎在iOS 8中无效。我试着弄清楚如何解决问题。有人知道在iOS 8中是否有办法做到这一点?

有一点值得注意的是,我正在执行此操作的应用有一个有效AVCaptureSession而我试图隐藏HUD(音量按钮充当相机上的快门)。不确定是否会有一些副作用。

3 个答案:

答案 0 :(得分:1)

好吧,这会滥用私有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];
    }
}

答案 1 :(得分:1)

在iOS 9中使用MPVolumeView

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

答案 2 :(得分:-1)

简化解决方案:

#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