How can I detect if an iOS device supports the blur effect?

后端 未结 2 1015
南笙
南笙 2021-01-02 07:46

It appears that different iOS devices render UINavigationBars with barStyle = UIBarStyleBlack and translucent = YES very differently.

相关标签:
2条回答
  • 2021-01-02 08:35

    What about this UIDevice category along with observing for UIAccessibilityReduceTransparencyStatusDidChangeNotification?

    @interface UIDevice (Additions)
    
    @property (readonly) NSString *platform;
    @property (readonly) BOOL canBlur;
    
    @end
    
    
    @implementation UIDevice (Additions)
    
    - (NSString *)platform {
        int mib[] = { CTL_HW, HW_MACHINE };
        size_t len = 0;
        sysctl(mib, 2, NULL, &len, NULL, 0);
        char *machine = malloc(len);
        sysctl(mib, 2, machine, &len, NULL, 0);
        NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
        free(machine);
    
        return platform;
    }
    
    - (BOOL)canBlur {
        if(NSStringFromClass([UIVisualEffectView class]) && UIDevice.currentDevice.systemVersion.floatValue >= 8.0 && !UIAccessibilityIsReduceTransparencyEnabled()) {
            NSString *platform = self.platform;
            CGFloat deviceVersion = [[[platform stringByReplacingOccurrencesOfString:@"[^0-9,.]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, platform.length)] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
    
            if([platform isEqualToString:@"i386"] || [platform isEqualToString:@"x86_64"]) {
                return YES;
            } else if([platform rangeOfString:@"iPhone"].location != NSNotFound) {
                return (deviceVersion >= 4.1);
            } else if([platform rangeOfString:@"iPod"].location != NSNotFound) {
                return (deviceVersion >= 5.1);
            } else if([platform rangeOfString:@"iPad"].location != NSNotFound) {
                return (deviceVersion >= 3.4);
            }
        }
    
        return NO;
    }
    

    Don't forget to #include in your implementation file.

    0 讨论(0)
  • 2021-01-02 08:44

    I think this question is what you want to do. Just to write conditional code for different devices:

    Determine device (iPhone, iPod Touch) with iPhone SDK

    0 讨论(0)
提交回复
热议问题