开发者

NSScrollView jumping to bottom on scroll

I have an NSScrollView containing an NSImageView, which resizes based on various factors. When it resizes I have generally changed the image, so I scroll the NSScrollView to the top. This works fine. However, when I start to scroll the NSScrollView again, it moves a few pixels and then (most of the time) jumps to the bottom of the scroll. After it jumps once, it works as normal until I move the scroller to the top again. This is driving me insane. All I'm really doing is this:

[_imageView setImage: anNSImage];

NSRect frame;

NSSize imageSize = [anNSImage] size];
frame.size = imageSize;
frame.origin = NSZeroPoint;

[_imageView setFrame: frame];
[[_mainScrollview contentView] scrollToPoint: NSMakePoint(0, [_imageView frame].size.height - [_mainScrollview fra开发者_JS百科me].size.height)];


Nick, thanks for your code. I modified your code and it works well now. First, you need to scroll the vertical scroller to top. Second, scroll the content view.

    // Scroll the vertical scroller to top
    if ([_scrollView hasVerticalScroller]) {
        _scrollView.verticalScroller.floatValue = 0;
    }

    // Scroll the contentView to top
    [_scrollView.contentView scrollToPoint:NSMakePoint(0, ((NSView*)_scrollView.documentView).frame.size.height - _scrollView.contentSize.height)];


To circumvent this bug you simply need to enable the scrollers and then set the hidden property of the scrollers. As NSScrollers inherit from NSView this just stops them from displaying themselves:

mainScrollView.hasHorizontalScroller = YES;
mainScrollView.hasVerticalScroller = YES;
mainScrollView.verticalScroller.hidden = YES;
mainScrollView.horizontalScroller.hidden = YES;

Works for me at least. Only tested on OS 10.7.2 though.

Does anyone have a radar filed on this one btw? They won't fix it if nobody tells them. :)


Seems I can use reflectScrolledClipView: instead of set scrollers' values to sync the position. Ex.

[self.scrollView.contentView scrollToPoint:NSMakePoint(300, 0)];
[self.scrollView reflectScrolledClipView:self.scrollView.contentView]; // synced then


My hacky solution. Turn on Show Vertical Scroller (like Chetan said, this is what triggers the bug). Send table view to back. Expand left edge of table view until the scroller is covered by whatever's to the right edge of it.


on macOS Sierra, [_scrollView.contentView scrollToPoint:]; was buggy and was causing erratic flickering. However using [_scrollView.documentView scrollPoint:] cleared up all of those issues for me. This is also the suggested method by Apple in their tutorial found here.


You set the scrollView.contentOffset = CGPointMake(0,0). Or according to your requirement. Set contectoffset after you change the image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜