1

I have an IKImageBrowserView that is the document view of an NSScrollView. How can I set a background image (a texture) so that the background remains fixed when the user scrolls.

I have tried a variety of things but either they just don't work or it doesn't remain fixed.

I have tried making the IKImageBrowserView and NSScrollView transparent, but this doesn't work, the background is black.

I have tried adding the texture as the background layer of the image browser:

NSColor* browserBackgroundColor = [NSColor colorWithPatternImage:[NSImage imageNamed:@"linenTile"]];
CALayer *layer = [CALayer layer];
[layer setBackgroundColor:[browserBackgroundColor CGColor]];
[self.imageBrowserView setBackgroundLayer:layer];

It remains fixed when scrolling down, but when scrolling up. This is the closest I have got it.

1
  • I actually need the opposite, the background to scroll with the view and it remains indeed fixed when scrolling down. Do you know how can I make it scroll in both directions ? Commented Feb 13, 2013 at 10:26

3 Answers 3

2

If your scrollView is full screen, you can just set you image to be the background of your self.view, and then set the background of your scrollView transparent.

Or you can just add a separate NSView beneath your scrollView, make it cover the same area as your scrollView, and set its background.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried making both the NSScrollView and IKImageBrowserView transparent but it causes artifacts and looks very bad, and results in a black background.
did you have a background view set up? and what kind of artifacts did you see?
I had a background view. Around the edges of the non rectangular images there was no gradient in the alpha, (This is not a problem with the image as if I set the background of the image browser it doesn't happen), it just was very jagged.
It seems that in different situations setting the background color of the document view does different things, sometimes it won't set the background color of the area you see when the scrollview bounces, sometimes it does...
you've tried to set your image to be the backgroundColor of your NSScrollView right?
1

Finally got the combination correct.

Subclass NSScrollView and draw the pattern image in drawRect:

- (void)drawRect:(NSRect)rect {
    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"linenTile"]] set];
    NSRectFill(rect);
}

Set the background of the document view to clear (for IKImageBrowserView like this):

[self.imageBrowserView setValue:[NSColor clearColor] forKey:IKImageBrowserBackgroundColorKey];

And also the Draw Background check box in interface builder on the scroll view must be unchecked. Or set the value to NO in code.

If you want the pattern to scroll with the content. Don't subclass the NSScrollView and just set the background color of the scrollview to the pattern, and the background color of the document view to clear.

Comments

0

Subclassing and -drawRect: is unnecessary, simply use the backgroundColor property of NSScrollView. The docs say "This color is used to paint areas inside the content view that aren’t covered by the document view."

scollView.backgroundColor = [[NSColor colorWithPatternImage:[NSImage imageNamed:@"linenTile"]] set];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.