1

I want multiple UIViewControllers holding a UICollectionView to look the same in constraints, size and cells, including textFields and UIImageView inside the cells.

This is how I want my views to look (roughly):

enter image description here

How can I decide of a uniform layout for all Collections ? (If one is changed - all others should be changed too)

jscs
  • 62,161
  • 12
  • 145
  • 186
Ofri
  • 259
  • 3
  • 15

1 Answers1

1

Two options depending on what you need:

  1. You need one base UIViewContoller class that holds a collection view and search bar - let's call it CollectionViewController - and make other view controllers inherit from it. Then a change in the CollectionViewContoller will apply to the other controllers. Unfortunately this cannot be done in storyboards. You can look here: How to use single storyboard uiviewcontroller for multiple subclass - suggestions are to use either a xib file to make a view with the needed subviews or make the whole view controller in code.
  2. Use only one view controller and change the data you load into it depending on what you need.
surToTheW
  • 614
  • 6
  • 25
  • Alright. Let ys say I have a base one. How do I style is for all of them? It should be programatically I take it. How do I do that? – Ofri Mar 04 '18 at 17:58
  • I don't understand the question completely. Are you asking how to create UICollectionView programatically or something else? – surToTheW Mar 04 '18 at 18:44
  • How to change its appearance (size, width), along with its cells and all inner cell views (Like images and text fields) – Ofri Mar 04 '18 at 18:48
  • (Programatically) – Ofri Mar 04 '18 at 18:48
  • 1
    OK - if I got your question - You can add constraints programatically - that would alter the size and width. After you create the cell views in code you change images and text fields the same way you would change them if they were created in storyboards - in cellForItemAtIndexPath or in the UICollectionView subclass itself - depending if you want the change for specific cells or for all. – surToTheW Mar 04 '18 at 18:52
  • Do you have an example in code ? English isn't my first language so it's easier for me to understand in code :) – Ofri Mar 04 '18 at 18:55
  • 1
    Here you can find some sample code: https://gist.github.com/anonymous/f4d6001cd9bdc87b4449c1ce36a1e6b1 – surToTheW Mar 04 '18 at 19:24
  • Looking at it now – Ofri Mar 05 '18 at 07:09
  • for some reason, after "ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)" I get that the UICollectionView is nil – Ofri Mar 05 '18 at 07:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166225/discussion-between-surtothew-and-ofri). – surToTheW Mar 05 '18 at 09:21
  • I have another question, if you might be able to help :) https://stackoverflow.com/questions/49137461/nserror-domain-com-google-httpstatus-code-404 – Ofri Mar 07 '18 at 09:05