Sanket
Sanket

Reputation: 11

Add/Remove constraints for specific size classes in Storyboards (iOS)

I have couple questions related to adding/removing constraints for specific size classes in UICollectionViewCell.

  1. I'm having a collection view with cell design for compact and regular - width and any - height. I have added appropriate constraints & it works with no issues on all devices. But the storyboard by default shows (any, any) size class & I haven't set any constraints here, hence it show errors. Am I suppose to just ignore these errors? If not, how can I fix them?

  2. As I programmatically define the size of the collection view item in the delegate, I need to invalidate the collection layout on size change when app is in iOS 9 split view(50-50) & rotated. Isn't this costly? Can I optimize? Note: The horizontal class doesn't change here.

Upvotes: 0

Views: 621

Answers (2)

Sanket
Sanket

Reputation: 11

Well, for the part 1 of my question, ideally we should have a design that works well with what Charles said above. But in my case I have a clear distinction between compact & regular width design. So, the solution I figured out was to disable(uninstall) my sub views under the collection view cell for (any, any). As I handled both (compact, any) & (regular, any) this seems to be a reasonable approach. For the second part I'm still invalidating the layout as mentioned before.

Upvotes: 0

Charles A.
Charles A.

Reputation: 11123

  1. Unfortunately Xcode isn't smart enough to determine that you've covered all the size class permutations (you might want to file a bug report). Best practice at this point is to pick what you consider your "default" layout and implement that as the Any/Any size class. If you decide that your default behavior is your Compact/Any layout for example, then you would implement that as Any/Any and put a specific implementation in for Regular/Any.

  2. It sounds like asking the collection view layout to invalidate is appropriate in this case. It can be costly depending on what you're displaying, but you don't have much of an alternative if all your items are changing size. Having said that, if only part of your layout is invalid, you can use invalidateLayoutWithContext(_:) instead of invalidateLayout to specifically callout what is being invalidated. You can read more about it here. It's also worth noting that the horizontal size class of an iPhone 6+ and iPhone 6s+ do change on rotation (Compact in Portrait and Regular in Landscape).

Upvotes: 1

Related Questions