0

I have this control called ITNavigationView on Github.

It's smoothly animating from one NSView to another by caching and adding them to a NSImageView. When caching the view, a subview centred in the x axis will be pulled exactly 1 pixel to the right. If I instead add a leading constraint, this doesn't happen.

How can I prevent this from happening?

To cache the view, I'm using this code:

- (NSImage *)imageOfView:(NSView *)view {
    [view layoutSubtreeIfNeeded];
    [view setNeedsUpdateConstraints:YES];
    [view updateConstraintsForSubtreeIfNeeded];

    NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:view.bounds];
    [view cacheDisplayInRect:view.bounds toBitmapImageRep:rep];

    return [[NSImage alloc] initWithCGImage:[rep CGImage] size:view.bounds.size];
}

EDIT

Also worth noting is that this only happens when the superview has an odd width.

IluTov
  • 6,807
  • 6
  • 41
  • 103
  • have you tried locking focus on an NSImage of correct size and calling the views `drawRect:` method? – Brad Allred Nov 11 '13 at 00:17
  • @BradAllred This will unfortunately not work for most layer-backed views. – IluTov Nov 11 '13 at 00:58
  • did you try anyway? I would have though the drawing methods do more or less what the `cacheDisplayInRect:` method does. what about the `renderInContext:` method of `CALayer`? Doesn't work with some kinds of layers/transforms/filter, but you don't specify any of that. – Brad Allred Nov 11 '13 at 01:27
  • @BradAllred I actually have not tried it yet. I might consider it if there's absolutely no way around. – IluTov Nov 11 '13 at 09:47
  • I know it's been a long time... but did you figure out why the subview moved by 1px? – jtbandes Sep 05 '15 at 04:58
  • @jtbandes I can't even recall ever having asked this question. Sorry... – IluTov Sep 05 '15 at 08:17

0 Answers0