iosoverlayopengl-es-2.0eaglview

iOS: Creating an overlay on top of an EAGLView / GLKView


I'm trying to create an overlay on top of a GLKView (effectively an EAGLView). I'm aware of the performance impact, but in my situation that's not a problem, since the scene is paused in the background, it merely needs to remain visible.

I've created a custom UIView called ReaderView whose only custom code is the following:

-(CALayer*)layer {
CATextLayer *textLayer = [[CATextLayer alloc] init];

// Layer settings.
[textLayer setCornerRadius:5.0f];

// Text settings.
[textLayer setFont:CGFontCreateWithFontName((CFStringRef)READING_FONT)];
[textLayer setFontSize:READING_FONT_SIZE];
[textLayer setAlignmentMode:kCAAlignmentJustified];
[textLayer setWrapped:YES];

return textLayer;
}

I've then called the following in a GLKViewController:

-(void)onMyCustomEvent {
if (_readerView==nil) {
    CGRect frame = [[self view] frame];
    frame.size.width *= 0.8f;
    frame.size.height *= 0.8f;
    _readerView=[[ReaderView alloc] initWithFrame:frame];
    [_readerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
[_readerView setText:[node content]];
[[self view] addSubview:_readerView];
}

NSLog has proven this method gets called and the reader view gets initialized. However nothing displays on top of the GLKView.

Any idea why this doesn't work?


Solution

  • Since you're adding it as a subview, you should use the bounds property instead of the frame property from the parent view, like this:

    CGRect frame = self.view.bounds;
    

    If that doesn't fix it, try setting the background color of the _readerView to something noticable to ensure that the problem isn't the content missing, like this:

    _readerView.backgroundColor = [UIColor redColor];
    

    Finally, if none of that solves it, then maybe directly adding subviews to an EAGL view is the problem. In that case, create a container view and add your EAGL view and your _readerView to that instead.