将 UIVisualEffectView 添加到 UITableView 的背景视图仅适用于模拟器而非设备

Adding UIVisualEffectView to UITableView's background view only works on simulator not device

我有一个 UITableView 和一个 UIImageView 作为 backgroundView。这就像一个魅力。但是,现在我正在尝试模糊此图像(使用 UIVisualEffectView),以便 tableView 的背景看起来模糊。 但是,我的代码以某种方式忽略了图像并简单地模糊了 table 视图的白色背景。这是代码:

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sidebarImage"]];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[blurEffectView setFrame:tempImageView.bounds];
[tempImageView addSubview:blurEffectView];
self.tableView.backgroundView = tempImageView;

编辑:经过更多测试后,我发现这适用于模拟器,但不适用于实际设备。我在下面附上了截图。更奇怪的是,当我注释掉 [tempImageView addSubview:blurEffectView];图像出现。这意味着图像正在被复制到 phone。

试试这个

 UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sidebarImage"]];
    UIBlurEffect *blurEffect;
    blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    UIVisualEffectView * blurEffectView;
    blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];


    blurEffectView.frame = tempImageView.bounds;
    [tempImageView addSubview:blurEffectView];
    self.tableView.backgroundView = tempImageView;

look at this

这一定是我遇到的最愚蠢的问题之一。显然,我在 iphone 设置中打开了 "Reduce Transparency"。因此问题。希望这能帮助其他一些试图节省电池的可怜人。

您可以检测何时在 iOS 8+ 中启用 "Reduce Transparency" 使用:

if (UIAccessibilityIsReduceTransparencyEnabled()) {
   ...
}

This blog post 更深入地检测 UIVisualEffectView 是否在设备上可用。