0

My custom collection cell contains WKWebView. And i want to make cell height automatically to size of my WKWebView. But all my attempts to turn on self sizing cells, failed:(

In my controller i do:

var p = self.htmlNotificationsCollectionView.collectionViewLayout as? NSCollectionViewFlowLayout
p?.estimatedItemSize = CGSize(width: self.htmlNotificationsCollectionView.frame.width, height: 120)

As documentation write then should trigger preferredLayoutAttributesFitting method:

class HTMLNotificationCollectionViewItem: NSCollectionViewItem, WKNavigationDelegate {
    override func preferredLayoutAttributesFitting(_ layoutAttributes: NSCollectionViewLayoutAttributes) -> NSCollectionViewLayoutAttributes {
        print("got it")
    }
}

But nothing works, method didn't call and my cells have fixed height = 120

Also i tried:

class HorizontallyFlushCollectionViewFlowLayout: NSCollectionViewFlowLayout {

    override func layoutAttributesForItem(at indexPath: IndexPath) -> NSCollectionViewLayoutAttributes? {
        let attributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? NSCollectionViewLayoutAttributes
        guard let collectionView = collectionView else { return attributes }
        attributes?.frame.size.width = collectionView.frame.width - sectionInset.left - sectionInset.right
        return attributes

    }

    override func layoutAttributesForElements(in rect: CGRect) -> [NSCollectionViewLayoutAttributes] {
        let allAttributes = super.layoutAttributesForElements(in: rect)
        return allAttributes.flatMap { attributes in
            switch attributes.representedElementCategory {
            case .item: return layoutAttributesForItem(at: attributes.indexPath!)
            default: return attributes
            }
        }
    }
}

But my preferredLayoutAttributesFitting in Item still doesn't call:

override func preferredLayoutAttributesFitting(_ layoutAttributes: NSCollectionViewLayoutAttributes) -> NSCollectionViewLayoutAttributes {
        print("DSADASDADASD")
        return layoutAttributes
    }

Added test project:

https://github.com/Arti3DPlayer/SCollectionViewTests

enter image description here

Arti
  • 7,356
  • 12
  • 57
  • 122
  • Are you expecting the cell to autosize to the height of the _contents_ of the webview? – bjtitus Jul 20 '17 at 15:51
  • yes, and first i want to start with height of cell, and then height of webview. Or you have solution ? – Arti Jul 20 '17 at 17:46
  • web views don't resize themselves. You will need to get the height of the content in the web view and set the height of the web view based on that. https://stackoverflow.com/questions/13858290/ios-resize-uiwebview-after-didfinishload-content-should-fit-without-scrolling – bjtitus Jul 20 '17 at 17:51
  • @bjtitus yes i know that, but i want to solve issue with self sized collection view first – Arti Jul 20 '17 at 19:19
  • Is there a project-example on swift with `NSCollectionView` self size cells maybe – Arti Jul 20 '17 at 19:20
  • Added test project: https://github.com/Arti3DPlayer/SCollectionViewTests – Arti Jul 21 '17 at 12:10

1 Answers1

1

As documentation write then should trigger preferredLayoutAttributesFitting method

Documentation for this method also has a Special Considerations section, that says:

In OS X 10.11, this method is never called.

If you want to customize size for each item, use collectionView(_:layout:sizeForItemAt:) method of NSCollectionViewDelegateFlowLayout protocol instead:

extension ViewController : NSCollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: NSCollectionView,
                        layout collectionViewLayout: NSCollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> NSSize {
        return NSSize(width: self.myCollectionView.bounds.width, height: 180)
    }
}
toma
  • 1,471
  • 12
  • 20
  • 1
    i need custom height based on cell height – Arti Jul 23 '17 at 12:23
  • @Atri It sims like Apple did not implement optional methods of `NSCollectionViewDelegateFlowLayout ` protocol in NSCollectionViewItem at all. And if they say you need to call parent's `preferredLayoutAttributesFitting` - there is no such method (checked under objective-c runtime on macOS 10.12.5). Why you can't store item's height separately in your model and use `NSCollectionViewDelegateFlowLayout ` API? – toma Jul 23 '17 at 13:21
  • but if my cell automatically resize every time ? – Arti Jul 23 '17 at 13:54
  • @Arti you can store a list of created views in a model as well. – toma Jul 23 '17 at 14:56
  • It's not being called for on 10.13 either for custom layout. (not flow) – pronebird Mar 10 '18 at 16:46