ios - How can I add a vibrancy effect to a subview? -
the code below works fine simple:
uivisualeffectview *blurview
but, when try add label has vibrancy effect breaks down in tears , errors. :(
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];
custom set frame method:
#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); }
the label doesn't show on screen, show if add simple subview blurview.
[_blurview addsubview:_titlelabel];
i'm using code in initialization method, cause of problem?
- (instancetype)init { self = [super init]; if (self) { // code... } return self; }
i need in initialization method in order consistent how write code.
edit 1:
the vibrancyview should have frame set.
vibrancyview.frame = cgrectmake(0, 0, _blurview.frame.size.width, _blurview.frame.size.height);
turns out example in source incomplete.
edit 2:
the label show has no vibrancy effect.
help!
you missing creation of vibrancy effect:
uivibrancyeffect * vibrancyeffect = [uivibrancyeffect effectforblureffect:effect];
later should pass vibrancy view:
uivisualeffectview * vibrancyview = [[uivisualeffectview alloc] initwitheffect: vibrancyeffect];
i've made class available on github simplefy boiler code needed create uivisualeffectview.
Comments
Post a Comment