0

How do I add size class customization pragmatically? I know how to do this in interface builder but would like to do this with my existing programmatic NSLayoutConstraints. Can't seem to find any info about this anywhere, except one or two places that said it could be done, but with no info about how.

Thanks

mikomi
  • 5
  • 5

2 Answers2

3

Getting trait collection from view is straightforward:

self.view.traitCollection

and UITraitCollection has horizontalSizeClass and verticalSizeClass properties that you can check and apply constraints as required.

You also want to implement traitCollectionDidChange: method to get changes caused by rotation

kviksilver
  • 3,824
  • 1
  • 26
  • 27
  • Thanks kviksilver. This is part of what I was looking for, but I am looking for a way to have constraints that are in code be installed or not for specific size classes. In interface builder each constraint is defaulted to be installed for all size classes. You can then change that and specify which size classes each constraint is installed for. So, how do I do that in code. And if your answer was how, can you please explain further? It appears to me that you showed a way to determine current size classes for views, but not how to install specific constraints for each size class. – mikomi Feb 25 '15 at 18:28
  • You can do simple if (self.view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) and add constraints programatically for UIUserInterfaceSizeClassCompact size class. same goes for regular. Any constraints you add outside of those checks would be valid for both size lasses you can read more on adding constraints programatically https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutinCode/AutoLayoutinCode.html – kviksilver Feb 25 '15 at 19:12
  • Yes, I understand how to add constraints programmatically. But from what you said, I would add constraints based on current size class and then have to change them when 'traitCollectionDidChange' is called. In interface builder you can install the constraints for specific size classes and then that's it. No more checking later and such. I have a solution for how to adjust for different orientations and devices and such now, but it is a little tedious and wanted to utilize the amazing ease of the interface builder option without having to recreate all of constraints and views in a storyboard. – mikomi Feb 25 '15 at 19:24
  • Yes, you are correct, interface builder makes size classes much easier... :-) easiest way i could think of is keeping a reference to constraint and changing it on rotation changes... – kviksilver Feb 25 '15 at 20:28
  • Yes, I do that now, but really don't like that and wanted to tap into this new awesome in code. I feel like there has to be a way... I'm still searching. Thanks for the pushing the right direction – mikomi Feb 25 '15 at 21:16
0

Mikomi, I think you want to check out the method UIAppearance.appearance(for:), it's iOS 8 or higher. It seems this is used by Interface Builder in Xcode.

bio
  • 669
  • 4
  • 14