如何为& quot;过冲& quot;上色NSCollectionView的背景 [英] How to Color the "Overshoot" Background of an NSCollectionView

查看:70
本文介绍了如何为& quot;过冲& quot;上色NSCollectionView的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使整个内容区域变黑.在我的情节提要中,我将包含集合视图的 NSScrollView NSClipView 的背景色设置为黑色,但不会改变外观.还尝试了编程替代方法:为滚动视图设置出口并调用:

  self.scrollview.backgroundColor = NSColor.blackself.scrollview.contentView.backgroundColor = NSColor.black 

...无效.

另外,我尝试了:

  collectionView.superview?.layer?.backgroundColor = NSColor.black.cgColor 

...但这也不起作用.

都不是:

self.view.layer?.backgroundColor = NSColor.black.cgColor 

或:

  self.view.window?.backgroundColor = NSColor.black 


更新:

我已将滚动视图的背景色设置为红色,并且 drawsBackground 设置为 true (在IB中和通过编程方式). NSScrollView backgroundColor 属性的文档说:

此颜色用于在内容视图内绘制未显示的区域被文档视图覆盖.

我在运行时已验证文档视图确实是我的集合视图:

 如果scrollview.documentView是NSCollectionView {打印(文档是集合")} 

但是,没有显示红色,并且文档(集合视图)之外的区域保持白色.


更新2:我触发了View Hierarchy Debugger,看来 drawsBackground 的值在运行时为false(我将其设置为 true 和情节提要中的两个代码)!:

(尽管背景颜色本身似乎有所反映)

解决方案

  1. 在Interface Builder中,在文档大纲中选择CollectionView.
  2. 在实用工具"窗口中,显示属性"检查器.
  3. 在原色"选择器控件中设置所需的颜色.

注意:您引用的Ray Wenderlich教程以编程方式设置了集合视图的背景色.您将要删除该行,或确保您设置的颜色与上述步骤3中选择的颜色相同.

I have implemented a collection view on my Cocoa app following Ray Wenderlich's tutorial (very helpful, given how buggy and broken Apple's API is in this area).

In the tutorial, the collection view's canvas is colored black using the following code (all code is in view controller class' viewDidLoad() method):

collectionView.layer?.backgroundColor = NSColor.black.cgColor

However, the area that "peeks" when you overshoot ("rubber-band") the scroll using -for example- a magic mouse/scrollwheel, is still white and very distracting:

I'm trying to make the whole content area black. In my storyboard, I set the background color of both the NSScrollView and NSClipView that contain the collection view to black, but it doesn't change the appearance. Also tried the programmatic alternative: setup an outlet for the scrollview and call:

self.scrollview.backgroundColor = NSColor.black
self.scrollview.contentView.backgroundColor = NSColor.black

...to no effect.

Additionally, I tried:

collectionView.superview?.layer?.backgroundColor = NSColor.black.cgColor

...but this doesn't work either.

Neither does:

self.view.layer?.backgroundColor = NSColor.black.cgColor

or:

self.view.window?.backgroundColor = NSColor.black


Update:

I have set the background color of the scroll view to red, and drawsBackground to true (both in IB and programmatically). The docs for NSScrollView's backgroundColor property say:

This color is used to paint areas inside the content view that aren’t covered by the document view.

I have verified at runtime that the document view is indeed my collection view:

if scrollview.documentView is NSCollectionView {
        print("Document is collection")
}

However, no red is displayed and the area beyond the document (collection view) stays white.


Update 2: I fired the View Hierarchy Debugger, and it seems that the value of drawsBackground is false at runtime (I set it to true in both code and storyboard)!:

(the background color itself seems to be reflected, though)

解决方案

  1. In Interface Builder, select the CollectionView in the document outline.
  2. In the Utilities window, show the Attributes inspector.
  3. Set the desired color in the Primary color selector control.

Note: the Ray Wenderlich tutorial that you referenced sets the background color of the collection view programmatically. You'll either want to remove that line, or ensure that you are setting the same color that you chose in step 3 above.

这篇关于如何为& quot;过冲& quot;上色NSCollectionView的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆