NSSplitViewController causing contained views to draw over window corners

前提是你 提交于 2019-12-12 09:57:18

问题


I'm trying to create a little Finder clone using Cocoa. I'm placing a source list table view (to act as a sidebar) and a standard table view inside of an NSSplitView controlled by an NSSplitViewController. Unfortunately, when I run this, the standard table view's white background is drawn over the bottom right corner of the window, resulting in a non-rounded corner:

I'm able to avoid the problem by not using NSSplitViewController, both with standard nib files or storyboards to get the desired result:

But… I really want to use NSSplitViewController. Is there any way to prevent this from happening?

Here is the Xcode workspace with the projects for the screenshots.


回答1:


I have seen such problem and it seems to be real bug. But there is workaround for that. You need make sure NSSplitViewController renders itself in view which is backed by CA layer. So to do this in your project, I added man-in-the-middle view controller with container view. Then I placed your split view inside of this container view. Finally parent view of the container view is set to be layer backed. Here is the picture:

After that I can see everything fine:

On another note: make sure you dont use 1000 priority of constraints until it is really required. I normally use 750 (I fixed that as well). Otherwise you will get a lot warnings about unsatisfiable things.

Here is you fixed project: https://github.com/emankovski/splitviewroundcorrners




回答2:


Make sure that root view in the view hierarchy to be layer-backed BEFORE adding it to view hierarchy.

Example. This works. Tested and confirmed to work in macOS 10.12 Sierra. Xcode 8.0.

split.view.wantsLayer = true
window.contentViewController = split
split.splitViewItems = [
    NSSplitViewItem(viewController: vc),
]

But this doesn't.

window.contentViewController = split
split.splitViewItems = [
    NSSplitViewItem(viewController: vc),
]
split.view.wantsLayer = true

I don't know what's happening here, and why this works. But let me speculate.

NSWindow without layer-backing root view just draws naively with no compositor involved. So they cannot mask out corners, and such artifacts appears. Just making them layer-backed will make it work. Just as like @Eugene pointed out.

Making everything layer-backed is not hard. Just make root view to be layer-backed, and every subviews in the hierarchy will be layer-backed to the leaf. (RTFM for details: CALayer.wantsLayer.)



来源:https://stackoverflow.com/questions/36464339/nssplitviewcontroller-causing-contained-views-to-draw-over-window-corners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!