在 NSScrollView 中仅自动隐藏水平滚动条
我有一个从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 NSScrollView 子类中尝试一下:
你需要选中“显示垂直滚动条”并取消选中“自动隐藏滚动条”才能使其工作;我没有费心使它能够适应 IB 的变化。另外,当窗口首次显示时(在 NSScrollView 构造函数中),您需要执行相同的操作。
我比较了有此更改和没有此更改的 CPU 使用率;在我的测试应用程序中,它似乎最多变化 1% (19%→20%)。
Give this a shot in your NSScrollView subclass:
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.