【问题标题】:Blur effect not properly adjusted to parent view模糊效果未正确调整到父视图
【发布时间】:2019-07-20 04:11:00
【问题描述】:

我有一个带有 imageView 的单元格,我想模糊这个图像以便为用户隐藏它。为此,我只是在其上添加了一个模糊视图,但它没有正确调整,并且右侧边缘有一条白色条纹。你知道发生了什么吗?这个问题只出现在物理设备上,在模拟器上就好了。这是我的模糊视图代码。

这是它目前的样子:

#import "BlurView.h"

@interface BlurView()
@property (nonatomic, strong) UIVisualEffectView *blurEffectView;
@end

@implementation BlurView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor clearColor];

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    self.blurEffectView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.blurEffectView];

    [[self.blurEffectView.widthAnchor constraintEqualToAnchor:self.widthAnchor] setActive:YES];
    [[self.blurEffectView.heightAnchor constraintEqualToAnchor:self.heightAnchor] setActive: YES];
    [[self.blurEffectView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor] setActive:YES];
    [[self.blurEffectView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor] setActive:YES];

    }
    return self;
}

@end

【问题讨论】:

  • 据我所知,您的约束是可以接受的,并且模糊视图看起来像是延伸到视图边缘。您的图像视图是否将图像内容拉伸到边缘?您的图像视图的内容模式设置为什么?
  • 显示添加此视图的代码。看起来您没有为 BlurView 本身添加约束。
  • 看起来在我的代码中我已经打开了shouldRasterize 属性,这是主要问题。当我关闭它时,现在一切看起来都很好。

标签: ios objective-c iphone autolayout


【解决方案1】:

首先我的建议是您应该始终尝试使用情节提要并在其中应用约束(这样您就不会遇到此类问题)。

如果您必须以编程方式进行,您可以尝试在 layoutSubviews 方法上应用模糊吗?

-(void) layoutSubviews{
    [super layoutSubviews];

//    your code here.

}

正如 Apple 所说 (https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews):只有当子视图的自动调整大小和基于约束的行为不能提供您想要的行为时,您才应该重写此方法。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 2017-03-28
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    相关资源
    最近更新 更多