You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS Objective-C:代码创建的NSScrollView出现异常滚动条元素求助

移除NSScrollView滚动条上方的未知方形元素(macOS Objective-C)

我在macOS平台使用Objective-C开发(非Swift、非iOS),通过代码创建了嵌套NSTableView的NSScrollView,现在滚动条上方出现一个方形元素,该元素在视图层级调试器中无法显示,希望将其移除。

问题演示

Weird scroll Bar element

视图层级截图

View hierarchy

相关代码

// Create tableView
NSTableView* tableView = [[NSTableView alloc] initWithFrame:NSZeroRect];
tableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.backgroundColor = [NSColor clearColor];
tableView.delegate = self.delegate ? self.delegate : weakSelf;
tableView.dataSource = self.delegate ? self.delegate : weakSelf;
tableView.rowHeight = height; // predefined
tableView.intercellSpacing = NSMakeSize(2, 2);
tableView.headerView = [[NSTableHeaderView alloc] initWithFrame:NSMakeRect(0, 0, 0, CGFLOAT_MIN)];
tableView.columnAutoresizingStyle = NSTableViewUniformColumnAutoresizingStyle;
if (@available(macOS 11.0, *)) {tableView.style = NSTableViewStylePlain;}
self.tableView = tableView;


// Create Scroll View
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];

scrollView.verticalScrollElasticity = NSScrollElasticityAutomatic;

// Setup scrollView
scrollView.drawsBackground = NO;
scrollView.hasVerticalScroller = YES;
scrollView.hasHorizontalScroller = YES;
scrollView.autohidesScrollers = YES;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.documentView = tableView;
scrollView.scrollerStyle = NSScrollerStyleLegacy; // Must be legacy (!)
scrollView.automaticallyAdjustsContentInsets = NO;
scrollView.contentInsets = insets;
scrollView.scrollerInsets = NSEdgeInsetsMake(-(insets.top), -(insets.left), -(insets.bottom), -(insets.right)); // insets are predefined
self.scrollView = scrollView;

[self.view addSubview:scrollView];

内容的提问来源于stack exchange,提问作者Pat_Morita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 04:32:02