Question

When I make an NSScrollView a subview of a layer-backed view (for example by choosing an NSTextView or NSTableView from IB), I see strange drawing behavior in the scroll view's document view.

To illustrate, I created a simple project with an NSTextView in a window. The only code I wrote is to turn on layer-backing for the window's content view:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[self.window contentView] setWantsLayer:YES];
}

This is the result when I type into the textview. The red underlines don't line up properly:

http://mowglii.com/random/screenshot.png

Also, the text and underlines jitter a lot when I resize the textview. I've seen the same jitter on resize when I use a tableview (inside a scrollview) instead of a textview.

Any idea what is going on?

Was it helpful?

Solution

NSScrollView indeed misbehaves badly when embedded in a layer-backed view.

Some serious trickery is needed to animate views containing scroll views. You could try turning on layer-backing only once you need to animate. You then need to force the drawing in order not to end up with an empty layer.

Usually, you will however need to go look much deeper in the back of tricks. Namely: Keep layer-backing switched off. Draw the view into an image, display that image in an overlay view with layer-backing enabled. Animate that view to an image of the final state. Then remove the overlay view to reveal the actual final state below.

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