iphoneipadmodalviewcontrollersize-classesadaptive-layout

Size classes on Modal Views


I am trying to set different constraints for iPad and iPhone (4'').

I have set Regular Height and Compact width constraints for iPhone. But these constraints are shown on 7.9'' iPad, 9.7'' iPad.

These constraints are for a modal view.

How do i make my Regular Height and Compact width constraints limit to my iPhones only.


Solution

  • Because the form sheet presentation on iPad is compact width and regular height, It is taking those constraints.

    Formsheet ios 8 constraints are same as iphones constraints

    The solution is to override traitCollection in the Presented view controller

    override var traitCollection: UITraitCollection
    {
        if UIDevice.isIPad()
        {
            let traits = UITraitCollection(horizontalSizeClass: UIUserInterfaceSizeClass.Regular)
            let traits2 = UITraitCollection(verticalSizeClass: UIUserInterfaceSizeClass.Regular)
            let traitCollection = UITraitCollection(traitsFromCollections: [traits, traits2])
    
            return traitCollection
        }
        else
        {
            return super.traitCollection
        }
    }