在 NSScrollView 中仅自动隐藏水平滚动条

发布于 2024-10-17 15:07:23 字数 1639 浏览 2 评论 0原文

我有一个从 IB 创建的 NSTableView,我只想自动隐藏水平滚动条。我想这样做的主要原因是因为似乎 NSTableView corverView 仅在存在垂直滚动条时才会显示。

我找不到任何方法来使用基类执行此操作。所以我尝试子类化 NSScrollView 并观察水平滚动条上的隐藏键(代码如下)。这有效;但是,每次用户调整窗口大小时,视图都会尝试重置当前可见选项。这使得我的实现有些昂贵;而且看起来不优雅。关于如何做到这一点有更好的想法吗?

提前致谢!

目前的实施:

@interface PVScrollView : NSScrollView {
  BOOL autohidesHorizontalScroller;
}

@property(assign) BOOL autohidesHorizontalScroller;

- (void) viewResized:(NSNotification*)notification;

@end

@implementation PVScrollView

@synthesize autohidesHorizontalScroller;

- (void) setAutohidesHorizontalScroller:(BOOL)val
{
  autohidesHorizontalScroller = val;
  [self setAutohidesScrollers:NO];
  [[self horizontalScroller] addObserver:self 
                              forKeyPath:@"hidden"
                                 options:0 
                                 context:nil];

}

- (void) observeValueForKeyPath:(NSString *)keyPath 
                       ofObject:(id)object 
                         change:(NSDictionary *)change 
                        context:(void *)context
{
  if (!([self documentVisibleRect].size.width < [[self documentView] frame].size.width) )
  {
    // remove observer
    [[self horizontalScroller] removeObserver:self
                                   forKeyPath:@"hidden"];
    [[self horizontalScroller] setHidden:YES];
    //[[self horizontalScroller] setNeedsDisplay:YES];
    // add it back
    [[self horizontalScroller] addObserver:self 
                                forKeyPath:@"hidden"
                                   options:0 
                                   context:nil];
  }
}

@end

I have an NSTableView, created from IB, that I want to only autohide the horizontal scroller on. The main reason I want to do this is because it seems the NSTableView corverView only get's displayed if there is a vertical scroller.

I can't find any method to do this with the base class. So I tried subclassing NSScrollView and observing the hidden key on the horizontal scroller (code below). This works; however, the view tries to reset the current visible options every time the user resizes the window. This makes my implementation somewhat expensive; and it seems inelegant. Any better ideas about how to do this?

Thanks in advance!

Current implementation:

@interface PVScrollView : NSScrollView {
  BOOL autohidesHorizontalScroller;
}

@property(assign) BOOL autohidesHorizontalScroller;

- (void) viewResized:(NSNotification*)notification;

@end

@implementation PVScrollView

@synthesize autohidesHorizontalScroller;

- (void) setAutohidesHorizontalScroller:(BOOL)val
{
  autohidesHorizontalScroller = val;
  [self setAutohidesScrollers:NO];
  [[self horizontalScroller] addObserver:self 
                              forKeyPath:@"hidden"
                                 options:0 
                                 context:nil];

}

- (void) observeValueForKeyPath:(NSString *)keyPath 
                       ofObject:(id)object 
                         change:(NSDictionary *)change 
                        context:(void *)context
{
  if (!([self documentVisibleRect].size.width < [[self documentView] frame].size.width) )
  {
    // remove observer
    [[self horizontalScroller] removeObserver:self
                                   forKeyPath:@"hidden"];
    [[self horizontalScroller] setHidden:YES];
    //[[self horizontalScroller] setNeedsDisplay:YES];
    // add it back
    [[self horizontalScroller] addObserver:self 
                                forKeyPath:@"hidden"
                                   options:0 
                                   context:nil];
  }
}

@end

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

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

发布评论

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

评论(1

遥远的她 2024-10-24 15:07:23

在你的 NSScrollView 子类中尝试一下:

- (void)setFrameSize:(NSSize)newSize;
{
    NSSize minFrameSize = [NSScrollView frameSizeForContentSize:[self contentSize] hasHorizontalScroller:NO hasVerticalScroller:YES borderType:[self borderType]];
    BOOL wantScroller = minFrameSize.width > newSize.width;
    [self setHasHorizontalScroller:wantScroller];        
    [super setFrameSize: newSize];
}

你需要选中“显示垂直滚动条”并取消选中“自动隐藏滚动条”才能使其工作;我没有费心使它能够适应 IB 的变化。另外,当窗口首次显示时(在 NSScrollView 构造函数中),您需要执行相同的操作。

我比较了有此更改和没有此更改的 CPU 使用率;在我的测试应用程序中,它似乎最多变化 1% (19%→20%)。

Give this a shot in your NSScrollView subclass:

- (void)setFrameSize:(NSSize)newSize;
{
    NSSize minFrameSize = [NSScrollView frameSizeForContentSize:[self contentSize] hasHorizontalScroller:NO hasVerticalScroller:YES borderType:[self borderType]];
    BOOL wantScroller = minFrameSize.width > newSize.width;
    [self setHasHorizontalScroller:wantScroller];        
    [super setFrameSize: newSize];
}

You'll need to check "Show Vertical Scroller" and uncheck "Automatically Hide Scrollers" for it to work; I didn't bother making it robust to changes in IB. Also, you'll need to do the same thing when the window is first displayed (in the NSScrollView constructor).

I compared CPU usage with and without this change; it seems to vary at most 1% (19%→20%) in my test application.

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