UICollectionViewCell - different layout for different size classes

almas

I have a simple UICollectionViewCell with one image and a label. In regular-regular size class I want the image to be on top, and the label below it. This is what it looks like in the Xcode's preview tab:

enter image description here

In any other size class I want the image to be on the left, and the label on the right:

enter image description here

I setup the constraints this way:

ImageView has following constraints:

  • leading and top constraints for Any-Any
  • fixed width and height for Any-Any

The label has following constraints:

  • trailing constraint to Superview - for Any-Any
  • top constraint to the ImageView - only for Regular-Regular
  • leading constraint to Superview - only for Regular-Regular
  • top constraint to Superview - for Any-Any but not for Regular-Regular
  • leading constraint to ImageView - for Any-Any but not for Regular-Regular

I also implemented the method to return the different cell size based on the current trait collection:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if (traitCollection.horizontalSizeClass == .Regular) {
      return CGSizeMake(240, 194)
    }
    else {
      return CGSizeMake(340, 128)
    }
  }

The cell looks fine in the preview, and everything works fine when I run it on iPhone (e.g Compact-Regular). However, auto layout breaks when I run it on iPad:

enter image description here

And of course I get a bunch of warnings in the debug console: Unable to simultaneously satisfy constraints.

So, I guess the question is - what is the proper way of setting up the cell for different size classes?

I created a github repo with a demo project

Thanks!

Jan Gorman

What you want to do is disable those constraints for the Regular-Regular size class like you have, but also change their priority to < 1000 (i.e. not required). That way it will instead use the higher priority constraints for the specific size class.

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Programmatically create auto layout constraints with different constants for different size classes

From Dev

Programmatically create auto layout constraints with different constants for different size classes

From Dev

Different layout for different screen size

From Dev

Different layout with different screen size with bootstrap 3

From Dev

Android Absolute Layout with different screen size

From Dev

HTML/CSS grid layout with different size boxes?

From Dev

How to know screen Size to different layout in android

From Dev

android constrain layout support different screen size

From Dev

AutoLayout Size Classes - different proportional heights

From Dev

iOS: Size classes not changing for different sizes

From Dev

Set Different Multiplier Value for Different Size Classes in Xcode Interface Builder?

From Dev

is it possible to provide different images for UIImageView on different size classes

From Dev

How to compare two different size array and give color in UICollectionviewcell as per compare?

From Dev

Fixed Layout EPUB where each page has a different size

From Dev

Layout weight not working properly in different screen size devices

From Dev

Can't change Constraint IBOutlet that is defined for different size classes in IB

From Dev

Programmatically implementing two different layouts using size classes

From Dev

Setting constraints in different size classes, view does not appear in landscape

From Dev

UILabel alignment and moving one view to other view in different Size Classes

From Dev

Setting constraints in different size classes, view does not appear in landscape

From Dev

UILabel alignment and moving one view to other view in different Size Classes

From Dev

Various autolayout constraints for different device orientations programmatically and without size classes?

From Dev

Cleaner way to instantiate different derived classes based on collection size?

From Dev

different keyboards have different layout

From Dev

Supporting different devices with different layout

From Dev

NLog different layout for different loggers

From Dev

Calculator app different layout on same screen size but different DPI (420 DPI vs 480 DPI)

From Dev

Is it possible to have different table view cell row heights for different size classes?

From Dev

TextSize for different device with different size?

Related Related

  1. 1

    Programmatically create auto layout constraints with different constants for different size classes

  2. 2

    Programmatically create auto layout constraints with different constants for different size classes

  3. 3

    Different layout for different screen size

  4. 4

    Different layout with different screen size with bootstrap 3

  5. 5

    Android Absolute Layout with different screen size

  6. 6

    HTML/CSS grid layout with different size boxes?

  7. 7

    How to know screen Size to different layout in android

  8. 8

    android constrain layout support different screen size

  9. 9

    AutoLayout Size Classes - different proportional heights

  10. 10

    iOS: Size classes not changing for different sizes

  11. 11

    Set Different Multiplier Value for Different Size Classes in Xcode Interface Builder?

  12. 12

    is it possible to provide different images for UIImageView on different size classes

  13. 13

    How to compare two different size array and give color in UICollectionviewcell as per compare?

  14. 14

    Fixed Layout EPUB where each page has a different size

  15. 15

    Layout weight not working properly in different screen size devices

  16. 16

    Can't change Constraint IBOutlet that is defined for different size classes in IB

  17. 17

    Programmatically implementing two different layouts using size classes

  18. 18

    Setting constraints in different size classes, view does not appear in landscape

  19. 19

    UILabel alignment and moving one view to other view in different Size Classes

  20. 20

    Setting constraints in different size classes, view does not appear in landscape

  21. 21

    UILabel alignment and moving one view to other view in different Size Classes

  22. 22

    Various autolayout constraints for different device orientations programmatically and without size classes?

  23. 23

    Cleaner way to instantiate different derived classes based on collection size?

  24. 24

    different keyboards have different layout

  25. 25

    Supporting different devices with different layout

  26. 26

    NLog different layout for different loggers

  27. 27

    Calculator app different layout on same screen size but different DPI (420 DPI vs 480 DPI)

  28. 28

    Is it possible to have different table view cell row heights for different size classes?

  29. 29

    TextSize for different device with different size?

HotTag

Archive