3

I am using an IKImageBrowserView to show a grid of images. I do not have any code that sets points or offsets or scrolls anything programmatically. I simply have code that lists the contents of a directory full of images and displays them in the image browser.

Upon launching the application, the scrolling view scrolls to the bottom of the grid of images, unprovoked. For a few seconds after this, if you try to scroll up with the mouse, the scroller glitches out and still try to scroll to the bottom. Thereafter, the issue stops and the user can scroll normally.

Prior to El Capitan, the auto-scroll was not happening.

Again, there is no code that scrolls the NSSrcollView or set the contentOffset, or scrollToRect, scrollToVisibleRect.

With that said, I have tried setting these values after the images have loaded with no success.

Question 1: Has anyone experienced this with NSScrollView or IKImageBrowserView on El Capitan (or possibly any Mac OS version)?

Question 2: Is there any way to debug why this is happening or how to get this to stop scrolling automatically?


Solution

This solution ended up working for me. Be sure to connect your outlets correctly.

.h

@interface WCSScrollView : NSScrollView

@end

.m

@class IKImageBrowserView;

@interface WCSScrollView ()
@property (strong) IBOutlet NSClipView * scrollerClipView;
@property (strong) IBOutlet IKImageBrowserView * imageBrowser;
@end

@implementation WCSScrollView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
}

- (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
    NSClipView * clip = self.subviews[0];
    self.imageBrowser = (IKImageBrowserView*)clip.subviews[0];
}

@end

View hierarchy

WrightsCS
  • 50,551
  • 22
  • 134
  • 186

3 Answers3

5

You need to subclass the NSScrollView

.h

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>

// This class fixes a glitch with the
// ikimagebrowserview
// http://stackoverflow.com/questions/33643289/cocoa-nsscrollview-with-ikimagebrowserview-scrolls-to-bottom

@interface IKImageBrowserScrollFixScrollView : NSScrollView

@end

.m

#import "IKImageBrowserScrollFixScrollView.h"

@implementation IKImageBrowserScrollFixScrollView

- (void)resizeSubviewsWithOldSize:(NSSize)oldSize {

    NSClipView * clipView;
    for (NSView * v in self.subviews) {
        if ([v isKindOfClass:[NSClipView class]]) {
            clipView = (NSClipView *)v;
            break;
        }
    }

    IKImageBrowserView * imageBrowser;
    for (NSView * v in clipView.subviews) {
        if ([v isKindOfClass:[IKImageBrowserView class]]) {
            imageBrowser = (IKImageBrowserView *)v;
            break;
        }
    }

    NSRect r = imageBrowser.frame;
    [super resizeSubviewsWithOldSize:oldSize];
    imageBrowser.frame = r;

}

@end
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
2

I had exactly the same problem but in a Xamarin.MAC project. My project had been put on ice for a few months and in between there were several Xamarin updates so I thought it was a Xamarin-related issue, not thinking about the fact that all my Macs had been upgraded in the meantime to El Capitan ! So you put me on the right track. At first I thought the Xamarin ImageKitDemo did not suffer from the issue but as soon as the HEIGHT of the IKImageBrowserView changed the scrolling begun as well. Experimenting with my project resulted in the same : as soon as the height changed (programmatically by setting Frame of Window or manually by resizing the window) the scrolling begins.

After spending some time I found a solution that actually works for me !

The trick seems to be to subclass NSScrollView, and override ResizeSubviewsWithOldSize() as shown below (Xamarin-code but should be easy to port to obj-C);

    public override void ResizeSubviewsWithOldSize (System.Drawing.SizeF oldSize)
    {
        NSClipView clip = this.Subviews [0] as NSClipView;
        myIKImageBrowserView vw = clip.Subviews [0] as myIKImageBrowserView;

        RectangleF frame = vw.Frame;
        base.ResizeSubviewsWithOldSize (oldSize);
        vw.Frame = frame;
    }

So basically I get the frame of the ImageBrowserView in the NSScrollView, call the base (super) function and then set the framesize back. Must be some El Capitan bug, the ImageKitDemo from Xamarin used to work before so it was not only my code behaving strangely.

Hope this will solve your issue as well !

Dirk
  • 21
  • 1
2

A band-aid is to subclass IKImageBrowserView and override adjustScroller with an implementation that does nothing. This will stop the view from constantly causing scrolling; however, I cannot say what unintended consequences overriding adjustScroller in this way may have. Additionally, in my application, the view is still initially scrolled to the bottom.

It's worth mentioning that Apple intends to deprecate IKImageBrowserView and instructs us to NSCollectionView, which is more general. If you are not invested in functionality specific to IKImageBrowserView, your time is probably better spent porting than looking for a workaround for this bug.

Jamborino
  • 161
  • 9