mdkr
mdkr

Reputation: 225

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.

Upvotes: 1

Views: 1052

Answers (2)

aduflo
aduflo

Reputation: 91

As a note, iOS Docs have this warning for traitCollection:

Use the traitCollection property directly. Do not override it. Do not provide a custom implementation.

With that known, here's an Obj-C solution which combines super traits w/ updated horizontal/vertical traits:

- (UITraitCollection *)traitCollection {
    UITraitCollection *traitCollection = [super traitCollection];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        UITraitCollection *horizontalTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
        UITraitCollection *verticalTraitCollection = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
        traitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[traitCollection, horizontalTraitCollection, verticalTraitCollection]];
    }
    return traitCollection;
}

Upvotes: 4

mdkr
mdkr

Reputation: 225

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
    }
}

Upvotes: 7

Related Questions