AppKit:如何移除NSOutlineView的1像素内边距
解决NSOutlineView与父视图间的1像素边框问题
检查
borderType属性
默认NSOutlineView的边框类型是NSBezelBorder,会自带一圈细边框。直接把它改成无边框就行:
代码实现:outlineView.borderType = .noBorder
用Storyboard/XIB的话,选中OutlineView,在右侧属性面板的Style栏里,把Border选项改成None。确保约束完全贴合父视图
给NSOutlineView添加top、leading、trailing、bottom四个约束,全部和父视图的对应边缘绑定,constant值设为0,优先级拉到最高(1000)。代码示例:outlineView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ outlineView.topAnchor.constraint(equalTo: parentView.topAnchor), outlineView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor), outlineView.trailingAnchor.constraint(equalTo: parentView.trailingAnchor), outlineView.bottomAnchor.constraint(equalTo: parentView.bottomAnchor) ])如果是IB里拖的约束,双击约束确认constant是0,没有偏移。
关闭滚动视图的自动内边距
NSOutlineView默认被NSScrollView包裹,滚动视图可能自动添加内边距。找到对应的NSScrollView,做两个设置:scrollView.automaticallyAdjustsContentInsets = falsescrollView.contentInsets = .zero排查父视图的额外设置
确认父视图没有设置layoutMargins,把它设为零:parentView.layoutMargins = .zero;另外检查父视图有没有添加layer边框,如果有的话也需要去掉。
内容的提问来源于stack exchange,提问作者ST K
相关产品推荐
相关产品推荐

