模糊效果未正确调整为父视图

时间:2019-02-26 15:07:31

标签: ios objective-c iphone autolayout

我的单元格带有imageView,我想模糊此图像以将其隐藏给用户。为此,我只是在上面添加了模糊视图,但是调整不正确,并且右边缘有白色条纹。你有什么想法吗?这个问题仅出现在物理设备上,在模拟器上就可以了。这是我的模糊视图代码。

这是当前的样子:Image with problem

#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

1 个答案:

答案 0 :(得分:0)

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

如果必须以编程方式执行此操作,可以尝试在layoutSubviews方法上应用模糊处理吗?

-(void) layoutSubviews{
    [super layoutSubviews];

//    your code here.

}

如Apple所说(https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews):仅当子视图的自动调整大小和基于约束的行为没有提供所需的行为时,才应覆盖此方法。

希望有帮助。