When exactly does an NSWindow get rounded corners?

雨燕双飞 提交于 2019-12-22 06:40:03

问题


(I found several similar questions, but nothing quite the same. Some were older than the OS in question. Some were doing crazy things, like completely custom windows. Nobody I found has instructions for how to make a perfectly ordinary window work correctly.)

Starting in OS X Lion, standard windows have had rounded corners. Unfortunately, I'm having trouble replicating this in my application. When an NSView is pushed against the corner of an NSWindow, sometimes it stays clipped to the round NSWindow, and sometimes it escapes and makes a square corner.

I haven't been able to figure out a pattern yet.

  • Even NSViews that "draw their background" sometimes force square corners, so it's not that.
  • My custom NSViews make square corners (am I responsible for checking my position in the NSWindow, and clipping to that, if I'm near the edge?), but some standard Cocoa controls do this, too.

For example, make a new Cocoa project, put a (scrolling) NSTableView or NSTextView in the main NSWindow, and add layout constraints so it follows the edges of the window. The bottom corners are square! The Finder, in contrast, has (what looks like) an NSTableView, right up against the bottom corner of the window, and it's rounded.

How do I make an ordinary NSWindow, with the proper round corners on the bottom?


回答1:


The flag to set, in order to get NSWindow to clip its contents to its actual window border (why the heck isn't this the default?) is NSView's wantsLayer.

  • In code: contentView!.wantsLayer = true, when the window is loaded.

  • Or in IB: select the root View of your Window, and then in "View Effects inspector", check the box next to it in "Core Animation Layer" (even if you're not using Core Animation).

Meta: the documentation for wantsLayer talks about a lot of complex things that don't seem at all related to window shape or clipping, and it's not even a property of NSWindow, while NSWindow has some flags that claim to be about window corner rounding but which don't work any more, so I'm not sure how anybody is supposed to discover this, except by spending 2 days on trial-and-error. I hope this answer helps somebody else!



来源:https://stackoverflow.com/questions/38986010/when-exactly-does-an-nswindow-get-rounded-corners

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