如何解决在 NSScroll 视图中以编程方式创建的 NSTableView 无法水平滚动
我使用的是 macOS 10.16 (BigSur) XCode 12,objective-c。
我以编程方式在 NSScroll 视图中创建了一个 NSTableView 并用虚拟数据填充它。 我可以向下滚动,而我不能左右滚动。所以我假设代码中有一些配置错误,但我无法弄清楚。
这是代码:
NSEdgeInsets insets = NSEdgeInsetsMake (0,0); // This is usually dynamic,just for demo purposes
NSEdgeInsets margins = NSEdgeInsetsMake (0,just for demo purposes
// Create tableView
NSTableView* tableView = [[NSTableView alloc] initWithFrame:NSZeroRect];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.backgroundColor = [NSColor clearColor];
tableView.delegate = self.delegate ? self.delegate : weakSelf;
tableView.dataSource = self.delegate ? self.delegate : weakSelf;
tableView.rowHeight = [SHDataTableCellView height];
tableView.intercellSpacing = NSMakeSize(2,2);
tableView.headerView = [[NSTableHeaderView alloc] initWithFrame:NSMakeRect(0,CGFLOAT_MIN)];
if (@available(macOS 11.0,*)) {tableView.style = NSTableViewStylePlain;}
self.tableView = tableView;
// Create Scroll View
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
// Setup scrollView
scrollView.drawsBackground = NO;
scrollView.hasVerticalScroller = YES;
scrollView.hasHorizontalScroller = YES;
scrollView.autohidesScrollers = NO;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.documentView = tableView;
scrollView.scrollerStyle = NSScrollerKnobStyleDefault;
scrollView.automaticallyAdjustsContentInsets = NO;
scrollView.contentInsets = insets;
scrollView.scrollerInsets = NSEdgeInsetsMake(-(insets.top),-(insets.left),-(insets.bottom),-(insets.right));
self.scrollView = scrollView;
[self.view addSubview:scrollView];
// Add constraints
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,scrollView);
NSDictionary *metrics = @{ @"marginLeft" : @(margins.left),@"marginRight" : @(margins.right),@"marginBottom" : @(margins.bottom),@"marginTop" : @(margins.top),@"insetLeft" : @(insets.left),@"insetRight" : @(insets.right),@"insetBottom" : @(insets.bottom),@"insetTop" : @(insets.top)};
[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(marginLeft)-[scrollView]-(marginRight)-|" options:0 metrics:metrics views:viewBindings]];
[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(marginTop)-[scrollView]-(marginBottom)-|" options:0 metrics:metrics views:viewBindings]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:60]]; // equals 2 rows of content
NSEdgeInsets margin 和 insets 在这个例子中设置为 0 往往被用作动态变量(你可以忽略它们)
解决方法
移除了宽度限制并且它起作用了。有时它太明显了,你仍然没有看到。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。