【发布时间】:2012-09-27 16:48:15
【问题描述】:
有没有一种简单的方法可以禁用 NSTableView 的滚动。
似乎没有任何属性
[myTableView enclosingScrollView] 或 [[myTableView enclosingScrollView] contentView] 禁用它。
【问题讨论】:
标签: objective-c macos scroll nstableview nsscrollview
有没有一种简单的方法可以禁用 NSTableView 的滚动。
似乎没有任何属性
[myTableView enclosingScrollView] 或 [[myTableView enclosingScrollView] contentView] 禁用它。
【问题讨论】:
标签: objective-c macos scroll nstableview nsscrollview
这对我有用:子类 NSScrollView,设置和覆盖通过:
- (id)initWithFrame:(NSRect)frameRect; // in case you generate the scroll view manually
- (void)awakeFromNib; // in case you generate the scroll view via IB
- (void)hideScrollers; // programmatically hide the scrollers, so it works all the time
- (void)scrollWheel:(NSEvent *)theEvent; // disable scrolling
@interface MyScrollView : NSScrollView
@end
#import "MyScrollView.h"
@implementation MyScrollView
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
[self hideScrollers];
}
return self;
}
- (void)awakeFromNib
{
[self hideScrollers];
}
- (void)hideScrollers
{
// Hide the scrollers. You may want to do this if you're syncing the scrolling
// this NSScrollView with another one.
[self setHasHorizontalScroller:NO];
[self setHasVerticalScroller:NO];
}
- (void)scrollWheel:(NSEvent *)theEvent
{
// Do nothing: disable scrolling altogether
}
@end
我希望这会有所帮助。
【讨论】:
这是我认为最好的解决方案:
import Cocoa
@IBDesignable
@objc(BCLDisablableScrollView)
public class DisablableScrollView: NSScrollView {
@IBInspectable
@objc(enabled)
public var isEnabled: Bool = true
public override func scrollWheel(with event: NSEvent) {
if isEnabled {
super.scrollWheel(with: event)
}
else {
nextResponder?.scrollWheel(with: event)
}
}
}
只需将任何NSScrollView 替换为DisablableScrollView(或BCLDisablableScrollView,如果您仍在使用ObjC)就完成了。只需在代码或 IB 中设置isEnabled,它就会按预期工作。
它的主要优点是嵌套滚动视图;在不将事件发送给下一个响应者的情况下禁用子级也会在光标位于禁用的子级上时有效地禁用父级。
这里列出了这种方法的所有优点:
NSScrollView
【讨论】:
isEnabled 提供默认的true 值时就停止了。无论哪种方式,这也可以在 ObjC 中使用而无需修改! :D
BCLDisablableScrollView 是什么?
感谢@titusmagnus 的回答,但我做了一个修改,以免在“禁用”滚动视图嵌套在另一个滚动视图中时中断滚动:当光标在边界内时,您无法滚动外部滚动视图内部滚动视图。如果你这样做......
- (void)scrollWheel:(NSEvent *)theEvent
{
[self.nextResponder scrollWheel:theEvent];
// Do nothing: disable scrolling altogether
}
...那么“禁用”的滚动视图会将滚动事件向上传递到外部滚动视图,并且它的滚动不会卡在其子视图中。
【讨论】:
为我工作:
- (void)scrollWheel:(NSEvent *)theEvent
{
[super scrollWheel:theEvent];
if ([theEvent deltaY] != 0)
{
[[self nextResponder] scrollWheel:theEvent];
}
}
【讨论】:
没有简单直接的方法(也就是说,没有像 UITableView 的 scrollEnabled 这样可以设置的属性),但我发现 this answer 在过去很有帮助。
您可以尝试的另一件事(不确定)是子类化NSTableView 并覆盖-scrollWheel 和-swipeWithEvent,所以它们什么都不做。希望这会有所帮助
【讨论】: