Skip to content

Fix CustomCollectionView-Swift sample#22

Merged
maicki merged 3 commits into
TextureGroup:masterfrom
george-gw:fixCustomCollectionViewSwiftSample
May 3, 2017
Merged

Fix CustomCollectionView-Swift sample#22
maicki merged 3 commits into
TextureGroup:masterfrom
george-gw:fixCustomCollectionViewSwiftSample

Conversation

@george-gw

Copy link
Copy Markdown
Contributor

The sample was crashing, the crash was reported on Slack.
This is a quick fix

@CLAassistant

CLAassistant commented Apr 18, 2017

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@maicki

maicki commented Apr 18, 2017

Copy link
Copy Markdown
Contributor

@Adlai-Holler Did this change due to our initializer changes?

@Adlai-Holler

Copy link
Copy Markdown
Member

@george-gw @maicki Could someone link to the Slack conversation about the issue, and open a GitHub issue for it? I searched around but couldn't find it.

@george-gw

Copy link
Copy Markdown
Contributor Author

@Adlai-Holler #39

@maicki

maicki commented Apr 28, 2017

Copy link
Copy Markdown
Contributor

@Adlai-Holler Any thoughts on it based on the issue? Did the initializer changed due to our changes?

@maicki

maicki commented May 3, 2017

Copy link
Copy Markdown
Contributor

Following up with that, this change does not have anything to do with the initializer change @Adlai-Holler did a while back.

The reason looking into it bit more actually is:
The collection node get's allocated with CGSize.zero in init and the frame get
s updated in viewWillLayoutSubviews in the view controller. Unfortunately a layout pass get’s through before viewWillLayoutSubviews occur and the frame of the collection node is CGSize.zero . Unfortunately the collection layout calculates the size of an item via collectionView.bounds.size.width - sectionInsets.width * 2 or similar. The result is that we get a minus width and the whole thing crashes as we try to layout a node that does not have a valid size.

Another resolution would be if we would not move to ASViewController is to do something like that:

  required init?(coder aDecoder: NSCoder) {
    let layout = MosaicCollectionViewLayout()
    layout.numberOfColumns = 3;
    layout.headerHeight = 44;
    _collectionNode = ASCollectionNode(frame: CGRect.zero, collectionViewLayout: layout)
    
    super.init(coder: aDecoder)
    
    layout.delegate = self
    
    _sections.append([]);
    var section = 0
    for idx in 0 ..< kNumberOfImages {
      let name = String(format: "image_%d.jpg", idx)
      _sections[section].append(UIImage(named: name)!)
      if ((idx + 1) % 5 == 0 && idx < kNumberOfImages - 1) {
        section += 1
        _sections.append([])
      }
    }
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    _collectionNode.frame = self.view.bounds;
    _collectionNode.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    _collectionNode.dataSource = self;
    _collectionNode.delegate = self;
    _collectionNode.backgroundColor = UIColor.white
    _collectionNode.registerSupplementaryNode(ofKind: UICollectionElementKindSectionHeader)
    _collectionNode.view.isScrollEnabled = true
    _collectionNode.view.layoutInspector = _layoutInspector
    self.view.addSubnode(_collectionNode!)
  }

@maicki maicki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@george-gw Thanks for fixing that. Will merge now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants