Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CustomCollectionView-Swift sample #22

Merged

Conversation

george-gw
Copy link
Contributor

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

@CLAassistant
Copy link

CLAassistant commented Apr 18, 2017

CLA assistant check
All committers have signed the CLA.

@maicki
Copy link
Contributor

maicki commented Apr 18, 2017

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

@Adlai-Holler
Copy link
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
Contributor Author

@Adlai-Holler #39

@maicki
Copy link
Contributor

maicki commented Apr 28, 2017

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

@maicki
Copy link
Contributor

maicki commented May 3, 2017

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!)
  }

Copy link
Contributor

@maicki maicki left a comment

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.

None yet

5 participants