简体   繁体   中英

Resizing and placing a rotated view in landscape: updating constraints or translating/changing frames?

I've a subview which I have placed at the bottom of the screen in portrait orientation:

在此处输入图片说明

I've set constraints in storyboard for this orientation to pin its leading, trailing, and bottom space to the superview, as well as a fixed height of the subview.

I want this subview to be at the right side of the screen when the device is in landscape orientation like this:

在此处输入图片说明

In my UIViewController subclass behind, I apply a M_PI/2 radians rotation to the subview in viewWillTransitionToSize:withTransitionCoordinator: , then I get this result:

在此处输入图片说明

I tried to place the subview to the right side of the screen as I want, so after the rotation I call [self.view setNeedsUpdateConstraints]; and tried to set new constraints for this orientation, but I'm not able to get the result I want. So, I'm not sure if I'm trying to do the wrong thing... I thought that setting new constraints to pin the subview to the right side of the screen, would resize its height and refresh its X and Y values... Or maybe the right way is to translate the rotated subview and change its frame?

How could I solve this?

Thanks

EDIT: This is an example of the constraints I'm programmatically setting for landscape:

NSLayoutConstraint *customSubviewConstraintTop = [NSLayoutConstraint constraintWithItem:self.customSubview
         attribute:NSLayoutAttributeLeading
         relatedBy:NSLayoutRelationEqual
         toItem:self.view
         attribute:NSLayoutAttributeLeading
         multiplier:1.0
         constant:0];

This is supposed to pin the top of the subview (left side before being rotated) to the top of the screen (right side of the screen when it was in portrait)... right?

EDIT 2: This is the code I have for landscape constraints in updateViewConstraints :

if (!isPortrait) {

    self.customSubView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI/2);

    [self.view removeConstraint:self.customSubViewConstraintL];
    [self.view removeConstraint:self.customSubViewConstraintR];
    [self.view removeConstraint:self.customSubViewConstraintB];
    [self.view removeConstraint:self.customSubViewConstraintH];

    [self.customSubView setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.customSubViewConstraintH = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:35.0];

    self.customSubViewConstraintL = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0.0];

    self.customSubViewConstraintR = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0.0];

    self.customSubViewConstraintB = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0.0];

    [self.view addConstraint:self.customSubViewConstraintH];
    [self.view addConstraint:self.customSubViewConstraintL];
    [self.view addConstraint:self.customSubViewConstraintR];
    [self.view addConstraint:self.customSubViewConstraintB];
}

You probably use screen-bounded constrains, such as BottomLayoutGuide. Use vertical spacing instead.

UPD: The problem is that you set rotation to you custom view in viewWillTransitionToSize:withTransitionCoordinator: . Just set the desired height and width instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM