NSCollectionView可以自动调整其子视图的宽度以显示一列 [英] Can NSCollectionView autoresize the width of its subviews to display one column

查看:149
本文介绍了NSCollectionView可以自动调整其子视图的宽度以显示一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSCollectionView ,其中包含 CustomViews 的集合。最初,它将子视图平铺为列和行,如网格。然后我将IB中的属性设置为1,所以现在它只是在行中一个接一个地显示它们。但是,即使我的 CustomView 是400px宽,它设置为自动调整大小, NSCollectionView 是400px宽,并且它已设置到1列,子视图的宽度约为80px。

I have an NSCollectionView that contains a collection of CustomViews. Initially it tiled the subviews into columns and rows like a grid. I then set the Columns property in IB to 1, so now it just displays them one after another in rows. However, even though my CustomView is 400px wide, it's set to autoresize, the NSCollectionView is 400px wide, and it's set to 1 column, the subviews are drawn about 80px wide.

我知道我可以通过调用来解决这个问题:

I know I can get around this by calling:

CGFloat width = collectionView.bounds.size.width;
NSSize size = NSMakeSize(width, 85);
[collectionView setMinItemSize:size];
[collectionView setMaxItemSize:size];

但是将此代码放在 awakeFromNib 方法中我的 WindowController 仅在程序启动时设置正确的宽度。当我调整窗口大小(并且我指定的 NSCollectionView 自动调整大小)时,CustomViews保持其初始设置的宽度。

But putting this code in the awakeFromNib method of my WindowController only sets the correct width when the program launches. When I resize the window (and the NSCollectionView autoresizes as I've specified), the CustomViews stay at their initially set width.

如果需要的话,我很乐意自己调整子视图的大小,但我对Cocoa很新,似乎找不到任何解释如何做这样的事情的文章。有人能指出我正确的方向吗?

I'm happy to take care of resizing the subviews myself if need be, but I'm quite new to Cocoa and can't seem to find any articles explaining how to do such a thing. Can someone point me in the right direction?

Anthony

推荐答案

Matt Gallagher精彩的博客 Cocoa with Love about 来解决这个问题。本周,他分享了一个单列视图的绑定详细信息,如下所示:

Matt Gallagher's wonderful blog Cocoa with Love is about to address this. This week, he shared the bindings details of a one-column view like the one in question:

http://cocoawithlove.com/2010/03/designing-view-with-bindings.html

下一个条目,他承诺分享其余的实施,这应该会给你你想要的东西。

Next entry, he promises to share the rest of the implementation, which should give you what you're looking for.

(I应该注意他是直接子类化NSView并重新实现许多NSCollectionView的功能。这似乎很常见;没有多少人喜欢子类化NSCollectionView。)

(I should note that he is subclassing NSView directly and re-implementing many of NSCollectionView's features. This seems to be common though; not many people are fond of subclassing NSCollectionView.)

<强>编辑:似乎他违背了他的承诺......我们从来没有收到那篇文章。请参阅下面的tofutim答案。

Seems he broke his promise... we never did receive that post. See tofutim's answer below instead.

这篇关于NSCollectionView可以自动调整其子视图的宽度以显示一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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