Search code examples
iosnslayoutconstraintios-autolayout

How to update constraint whenrotate to landscape


I have added a constraints to an UIImageView in this way

 //-----------Add constraints to rightoval image-----------------

NSArray *constr_V2=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[rightOval(210)]|"
                                                          options:0
                                                          metrics:nil views:back];

NSArray *constr_H2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-rgtOvalX-[rightOval(65)]|"
                                                            options:NSLayoutFormatDirectionLeadingToTrailing
                                                            metrics:metrices
                                                              views:back];

[self.view addConstraints:constr_V2];
[self.view addConstraints:constr_H2];

This rgtOvalXvalue I calculated in this way

float rgtX=dm.screenWidth-65;

65 is my image view width. Its fine in portrait view. But in landscpe view its still takes X position same as the portrait X position. How can I update the constraint when the phone rotates to landscape mode. Please help me. Thanks


Solution

  • First of all - you will need to change the way you create constr_H2, cause you need to have access to constraint, that places rightOval left edge.

    NSArray *constr_H2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-rgtOvalX-[rightOval(65)]|"
                                                            options:NSLayoutFormatDirectionLeadingToTrailing
                                                            metrics:metrices
                                                              views:back];
    //This constant should be available in all view controller. Make property, for example
    NSLayoutConstraint *rightOvalLeftEdge = [NSLayoutConstraint constraintWithItem:<#rightOval#>
                                                                         attribute:NSLayoutAttributeLeft
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:<#rightOvalSuperView#>
                                                                         attribute:NSLayoutAttributeLeft
                                                                        multiplier:1.
                                                                          constant:0];
    

    Second, in your viewController in method -traitCollectionDidChange:

    - (void) traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
        [super traitCollectionDidChange:previousTraitCollection];
    
    
        if(previousTraitCollection.horizontalSizeClass != self.traitCollection.horizontalSizeClass) {
            if(self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact)
                self.rightOvalLeftEdge.constant = <#Calculate space in portrait orientation#>
            else if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular)
                self.rightOvalLeftEdge.constant = <#Calculate space in album orientation#>
        }
    }