2015-05-29 39 views
1

下面的代码的简单工作正常:如何为子视图添加活力效果?

UIVisualEffectView *blurView 

但是,当我尝试添加一个标签给它有它的眼泪和错误打破了一个充满活力的效果。 :(

UIBlurEffect * effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; 

    _blurView = [[UIVisualEffectView alloc] initWithEffect:effect]; 
    _blurView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5]; 
    [self addSubview:_blurView]; 

    UIVisualEffectView * vibrancyView = [[UIVisualEffectView alloc] initWithEffect:effect]; 

    [_blurView.contentView addSubview:vibrancyView]; 

    _titleLabel = [[UILabel alloc] init]; 
    _titleLabel.font = [UIFont boldSystemFontOfSize:17.0]; 
    _titleLabel.textColor = [UIColor colorWithWhite:0.1 alpha:0.5]; 
    _titleLabel.text = @"TITLE"; 

    [vibrancyView.contentView addSubview:_titleLabel]; 

自定义设置框架方法:

#pragma mark - 

- (void)setFrame:(CGRect)frame 
{ 
    [super setFrame:frame]; 

    _blurView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); 

    _titleLabel.frame = CGRectMake(16, 16, 200, 30); 
} 

标签不显示在屏幕上,但它确实表明了,如果我添加它作为一个简单的子视图的blurView

[_blurView addSubview:_titleLabel]; 

我正在使用的代码在初始化方法,可以是这个问题的原因是什么?

- (instancetype)init 
{ 
    self = [super init]; 

    if (self) { 

     // Code... 
    } 

    return self; 
} 

我需要它在初始化方法,以便与我如何编写代码一致。

Source

EDIT 1:

的vibrancyView也应该有它的帧集合。

vibrancyView.frame = CGRectMake(0, 0, _blurView.frame.size.width, _blurView.frame.size.height); 

原来,例如在源是不完整的

编辑2:

标签确实出现,但它有没有活力的效果。

帮助!

回答

1

你缺少的活力影响的创建:

UIVibrancyEffect * vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:effect]; 

之后,你应该把它传递给活力观点:

UIVisualEffectView * vibrancyView = [[UIVisualEffectView alloc] initWithEffect: vibrancyEffect]; 


我提供一个类上github简单的所有锅炉代码都需要创建一个UIVisualEffectView。

相关问题