Question

I am using the TapkuLibrary's otherwise excellent TKCalendarDayEventView and trying to selectively round one of the corners in the view as StuDev demonstrates here. Unfortunately, applying StuDev's code snippet results in the EventView disappearing entirely from its containing TKCalendarDayTimelineView. I am adding this code snippet underneath the current code in the

+ (id)eventViewWithFrame:(CGRect)frame id:(NSNumber *)id startDate:(NSDate *)startDate endDate:(NSDate *)endDate title:(NSString *)title location:(NSString *)location;

method. I have commented out code that otherwise sets the border width, color, or radius in the code. I have made sure that TKCalendarDayEventView doesn't have any superlayers, since the apple docs warn against adding masks to layers with superlayers:

When setting the mask to a new layer, the new layer’s superlayer must first be set to nil, otherwise the behavior is undefined.

I have also tried playing around with the backgroundColor and fillColor properties of the maskLayer. I don't see anything in the TKCalendarDayEventView that might stop this mask from being correctly applied. What could I be doing wrong?

Was it helpful?

Solution

If you put a breakpoint in your eventViewWithFrame:id:startDate:endDate:title:location: method, you will see that when you create your event view you are setting the frame to CGRectZero. The code snippet that then sets the rounded corner mask is using CGRectZero as the mask layer's frame.

Probably the simplest way to deal with this would be to override TKCalendarDayEventView's setFrame: method like so:

- (void)setFrame:(CGRect)newFrame
{
    if (!CGRectEqualToRect([super frame], newFrame)) {
        [super setFrame:newFrame];

        // Change the view's mask layer to fit the new frame.
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds 
                                                       byRoundingCorners:UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(15.0, 15.0)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

This way, every time you change the frame of the view the mask automatically adjusts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top