IKImageView 和滚动条

发布于 2024-08-17 23:14:28 字数 1274 浏览 12 评论 0原文

我正在尝试使用苹果提供的 IKImageViewDemo (http:// developer.apple.com/mac/library/samplecode/IKImageViewDemo/index.html),我正在尝试向其添加滚动条。我尝试了两件事:

1)将 IKImageView 嵌入 ScrollView 中。这产生了各种奇怪的效果,比如图像不再位于它应该在的位置,并且滚动条似乎位于固定位置,无论窗口有多大(所以我可以缩小窗口并丢失滚动条,即使滚动视图设置为随窗口调整大小)

2)我将 [_imageView setHasHorizo​​ntalScrollers: YES] (和垂直)添加到 openImageURL 方法中的代码中。这似乎什么也没做。

我错过了一些明显的东西吗?

另外:为什么

NSLog(@"scrollbar? H %d V %d hide %d", 
      _imageView.hasHorizontalScroller, 
      _imageView.hasVerticalScroller,
      _imageView.autohidesScrollers);

_imageView.hasHorizontalScroller = YES;
_imageView.hasVerticalScroller = YES;
_imageView.autohidesScrollers = YES;

NSLog(@"scrollbar? H %d V %d hide %d", 
      _imageView.hasHorizontalScroller, 
      _imageView.hasVerticalScroller,
      _imageView.autohidesScrollers);

给我:

scrollbar? H 0 V 0 hide 0
scrollbar? H 0 V 0 hide 0

另外:

同样地,为什么:

 BOOL b = _imageView.autohidesScrollers = YES;
 NSLog (@"b %d scrollers %d", b, _imageView.autohidesScrollers);

print b 1 scrollers 0 ?

I'm trying to use the IKImageViewDemo provided by apple (http://developer.apple.com/mac/library/samplecode/IKImageViewDemo/index.html) and I'm trying to add scrollbars to it. I've tried two things:

1) embedding the IKImageView in a ScrollView. This had all sorts of weird effects, like the image was no longer located where it should have been, and the scrollbars seemed to be in a fixed place, no matter how big the window was (So I could shrink the window and lose the scrollbars, even though the scrollview was set to resize with the window)

2) I added [_imageView setHasHorizontalScrollers: YES] (and vertical) into the code in the openImageURL method. This appears to have done nothing.

Am I missing something obvious?

Additionally: Why does

NSLog(@"scrollbar? H %d V %d hide %d", 
      _imageView.hasHorizontalScroller, 
      _imageView.hasVerticalScroller,
      _imageView.autohidesScrollers);

_imageView.hasHorizontalScroller = YES;
_imageView.hasVerticalScroller = YES;
_imageView.autohidesScrollers = YES;

NSLog(@"scrollbar? H %d V %d hide %d", 
      _imageView.hasHorizontalScroller, 
      _imageView.hasVerticalScroller,
      _imageView.autohidesScrollers);

give me:

scrollbar? H 0 V 0 hide 0
scrollbar? H 0 V 0 hide 0

?

Additionally additionally:

Equivalently why does:

 BOOL b = _imageView.autohidesScrollers = YES;
 NSLog (@"b %d scrollers %d", b, _imageView.autohidesScrollers);

print b 1 scrollers 0 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-08-24 23:14:28

IKImageViewDemo 中可能让您感兴趣的一件事是图像被缩放以适合 windowDidResize: 方法 ([_imageView ZoomImageToFit: self])。

将 IKImageView 嵌入到 NSScrollView 中是正确的做法。为了让滚动条在调整窗口大小时跟随窗口,您需要在 Interface Builder 中调整弹簧和支柱(==自动调整大小掩码)。

附录:正如您所注意到的,Mac OS X 10.6 中存在一个错误,导致此功能无法正常工作。您可以通过对 NSScrollView 进行子类化来解决该问题,如下所示:

@interface IKImageClipView : NSClipView
- (NSRect)docRect;
@end

@implementation ScrollViewWorkaround

- (void)reflectScrolledClipView:(NSClipView *)cView;
{
    NSView *_imageView = [self documentView];
    [super reflectScrolledClipView:cView];
    if ([_imageView isKindOfClass:[IKImageView class]] &&
         [[self contentView] isKindOfClass:[IKImageClipView class]] &&
         [[self contentView] respondsToSelector:@selector(docRect)]) {
        NSSize docSize = [(IKImageClipView *)[self contentView] docRect].size;
        NSSize scrollViewSize = [self contentSize];
        // NSLog(@"doc %@ scrollView %@", NSStringFromSize(docSize), NSStringFromSize(scrollViewSize));
        if (docSize.height > scrollViewSize.height || docSize.width > scrollViewSize.width)
         ((IKImageView *)_imageView).autohidesScrollers = NO;
        else
         ((IKImageView *)_imageView).autohidesScrollers = YES;
    }
}

@end

尝试一下:

http:// dl.dropbox.com/u/1583683/IKImageViewDemo.zip

这是带有滚动条和上述解决方法的 IKImageViewDemo 版本。

One thing that may have been catching you up in IKImageViewDemo was that the image was zoomed to fit in the windowDidResize: method ([_imageView zoomImageToFit: self]).

Embedding the IKImageView in a NSScrollView is the right thing to do. In order to get the scrollbars to follow the window as you resize it, you need to adjust the springs and struts (== autoresizing mask) in Interface Builder.

Addendum: As you've noticed, there is a bug in Mac OS X 10.6 that causes this not to work properly. You can work around the problem by subclassing the NSScrollView as follows:

@interface IKImageClipView : NSClipView
- (NSRect)docRect;
@end

@implementation ScrollViewWorkaround

- (void)reflectScrolledClipView:(NSClipView *)cView;
{
    NSView *_imageView = [self documentView];
    [super reflectScrolledClipView:cView];
    if ([_imageView isKindOfClass:[IKImageView class]] &&
         [[self contentView] isKindOfClass:[IKImageClipView class]] &&
         [[self contentView] respondsToSelector:@selector(docRect)]) {
        NSSize docSize = [(IKImageClipView *)[self contentView] docRect].size;
        NSSize scrollViewSize = [self contentSize];
        // NSLog(@"doc %@ scrollView %@", NSStringFromSize(docSize), NSStringFromSize(scrollViewSize));
        if (docSize.height > scrollViewSize.height || docSize.width > scrollViewSize.width)
         ((IKImageView *)_imageView).autohidesScrollers = NO;
        else
         ((IKImageView *)_imageView).autohidesScrollers = YES;
    }
}

@end

Try this out:

http://dl.dropbox.com/u/1583683/IKImageViewDemo.zip

It's a version of IKImageViewDemo with scroll bars and the above workaround.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文