でNSCollectionView autoresizeの幅のsubviewsを表示一列
-
22-09-2019 - |
質問
私は NSCollectionView
このコレクションを含みま CustomViews
.当初はタイル張りのsubviewsへの列と行のようにグリッドそこでの設定 Columns
物件IB(1)にすると、こで示したもとにされています。しかし、もっ CustomView
は400pxしているため、何かあったのに設定autoresize、 NSCollectionView
は400px広く、設定値が1カラムには、subviewsが描かれてい80pxます。
思いつき周辺のこの呼び出:
CGFloat width = collectionView.bounds.size.width;
NSSize size = NSMakeSize(width, 85);
[collectionView setMinItemSize:size];
[collectionView setMaxItemSize:size];
でもこのコードを awakeFromNib
方法は私の WindowController
みの訂正幅がプログラムの提供を開始。私はサイズ変更のウィンドウの NSCollectionView
autoresizesしていることが特徴となっ指定)、CustomViews滞在を初期設定の幅になります。
嬉しいのサイズ変更のsubviews自分に必要であれば、私は非常に新しいココアができないよう記載する方法を説明するなどというもの。できる人もの方向になっていますか?
アンソニー
解決
マット-ヴィメタルバンドの素晴らしいブログ ココアの愛 は 報 住ます。今週、共用のバインディングの詳細については、カラムビューのような質問
http://cocoawithlove.com/2010/03/designing-view-with-bindings.html
次回は彼が約束を共有し、実施するべきだ。
(なおその場合は、サブクラスNSView直接再実施の多くはNSCollectionViewのが特徴です。といっていいのかもしれない共通のもの;しない多くの人が好きな人の場合は、サブクラスNSCollectionView.)
編集: うた約束...しなかった場合には送付されます。見tofutimの答え以下です。
他のヒント
本当の答えは、0,0(NSZeroSize
)にmaxItemSizeを設定することです。それ以外の場合は、それが計算されます。
[self.collectionView setMaxItemSize:NSZeroSize];
このはawakeFromNib
に設定することができます。
ごきげんよう、トメ子です、 非常に 対応の遅れも同じ問題といっ解決すか.値段が高くなりますが、アクセスの範囲を同封のスクロールのメッセージ-アドバイスのコレクションビューです。なのでそれを解決するまで交換する必要があり初のライン:
CGFloat width = collectionView.enclosingScrollView.bounds.size.width;
私は、デフォルトのレイアウトで動作するようにこれを取得することができませんでした - しかし、それはカスタムレイアウトを実装するために非常に簡単です。
/// Simple single column layout, assuming only one section
class SingleColumnLayout: NSCollectionViewLayout {
/// Height of each view in the collection
var height:CGFloat = 100
/// Padding is wrapped round each item, with double an extra bottom padding above the top item, and an extra top padding beneath the bottom
var padding = EdgeInsets.init(top: 5, left: 10, bottom: 5, right: 10)
var itemCount:Int {
guard let collectionView = collectionView else {
return 0
}
return collectionView.numberOfItems(inSection:0)
}
override func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
return true
}
override open func layoutAttributesForItem(at indexPath: IndexPath) -> NSCollectionViewLayoutAttributes? {
let attributes = NSCollectionViewLayoutAttributes(forItemWith: indexPath)
guard let collectionView = collectionView else {
return attributes
}
let bounds = collectionView.bounds
let itemHeightWithPadding = height + padding.top + padding.bottom
let row = indexPath.item
attributes.frame = NSRect(x: padding.left, y: itemHeightWithPadding * CGFloat(row) + padding.top + padding.bottom , width: bounds.width - padding.left - padding.right , height: height)
attributes.zIndex = row
return attributes
}
//If you have lots of items, then you should probably do a 'proper' implementation here
override open func layoutAttributesForElements(in rect: NSRect) -> [NSCollectionViewLayoutAttributes] {
var attributes = [NSCollectionViewLayoutAttributes]()
if (itemCount>0){
for index in 0...(itemCount-1) {
if let attribute = layoutAttributesForItem(at: NSIndexPath(forItem: index, inSection: 0) as IndexPath) {
attributes.append(attribute)
}
}
}
return attributes
}
override open var collectionViewContentSize: NSSize {
guard let collectionView = collectionView else {
return NSSize.zero
}
let itemHeightWithPadding = height + padding.top + padding.bottom
return NSSize.init(width: collectionView.bounds.width, height: CGFloat(itemCount) * itemHeightWithPadding + padding.top + padding.bottom )
}
}
すべてのあなたの必要性である。
var layout=SingleColumnLayout()
collectionView.collectionViewLayout = layout
別の後半1 - 。私はちょうどNSTableView
を使用してNSView
法によりdelegate
の提供に切り替え
自動サイズ変更が自由のために来て、1列は簡単ですが、それは大規模速くレンダリングます。
あなたが設定する必要があり、あなたは200x180pxのサイズでごCollectionViewItemをしたいとしましょう
[myCollectionView setMinItemSize:NSMakeSize(200, 180)];
[myCollectionView setMaxItemSize:NSMakeSize(280, 250)];
あなたの最大サイズは、見た目の良いに十分に大きいこととcollectionView-幅に合わせてストレッチのための十分なスペースを与える必要があります。
あなたは列の固定数を持っている場合は、おそらく(0,0)を使用することができますが、あなたは私が望んでいたように、行と列の動的な数をしたい場合..あなたは、固定分サイズと大きな最大を設定する必要があります.sizeます。
あなたが望むように動作するコレクションビューを取得するかもしれませんが、私はあなたが設計上の問題を抱えていると思います。
あなたはNSTableView
とセットの列1に、何にそのサイズが、「なし」を使用する必要があります。 NSTableView
は、表形式のデータを対象とし、それに加えてアイテムの大量のための素晴らしいパフォーマンスの向上を与える細胞を再利用することができます。 NSCollectionView
は、垂直スクロールと、より多くのグリッドに並べ項目線形レイアウトと同様です。列番号は、通常、デバイスの向きと画面サイズに応じて、より多くの項目が画面に表示されるように動的に変更することができるときに有用である。