How to change layout constraints programmatically on display rotation

对着背影说爱祢 提交于 2020-01-25 15:50:49

问题


For an app (iOS 7 compatible) I need to have the possibility for totally different layouts in landscape and portrait mode. I see the only way in setting the constraints programmatically and change them on rotation. So I set my interface using IB and then set the constraints programmatically.

I saw somewhere the approach to remove first all constraints, then create and add new constraints and then set the update flag for constraints on the parent view. I want the elements just to be full width of the screen and one underneath the other:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    self.landscapeConstraints = [NSMutableArray new];
    self.portraitConstraints = self.view.constraints;

    //creation of the viewsDictionary

    [self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[segmentedControl]-|" options:0 metrics:nil views:viewsDictionary]];
    [self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[fullText]-|" options:0 metrics:nil views:viewsDictionary]];
    [self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[bubbleTitle]-|" options:0 metrics:nil views:viewsDictionary]];
    [self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[bubble]-|" options:0 metrics:nil views:viewsDictionary]];
    [self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[chartView]-|" options:0 metrics:nil views:viewsDictionary]];
    [self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(20)-[segmentedControl]-[fullText]-[bubbleTitle]-[bubble]-[chartView]|" options:0 metrics:nil views:viewsDictionary]];

    [self.view removeConstraints:self.portraitConstraints];
    [self.view addConstraints:self.landscapeConstraints];

    [self.view setNeedsUpdateConstraints];
}

My question: Is this way complete and correct?

I come to this question, since the elements are too big after the rotation. It looks like the size of the superview is not correct after rotation. All elements are therefore too big and it seems that the biggest element (a long text label) is giving the width, which is a lot bigger than the screen. If I set one of the elements with a size in the constraints (eg. [segmentedControl(==500)] then the size of this element is ok and ALL other elements also have that size.

The command

NSLog(@"%@", [self.view performSelector:@selector(_autolayoutTrace)]);

gives me something like this for all elements:

*UIView:0x7f9a38f36510- AMBIGUOUS LAYOUT for UIView:0x7f9a38f36510.minX{id: 9}, UIView:0x7f9a38f36510.minY{id: 12}, UIView:0x7f9a38f36510.Width{id: 15}, UIView:0x7f9a38f36510.Height{id: 17}

Can anyone hint me where I am doing wrong? I am working on this for hours now...

EDIT

What I really don't understand: It seems the solution after I removed all constraints would be to define the size of the superview also by a constraint like this:

NSDictionary *viewsDictionary = @{ @"view":self.view};

[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view(360)]" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(650)]" options:0 metrics:nil views:viewsDictionary]];

Otherwise all views collapse, since the superview seems to have zero-size. And even with these additional constraints the view sits too far left (about 20px)!

Can anyone explain that to me? Do I really need to generate that constraint manually with the current size of the windows?? I would expect the containing view (which is the main window) to have the size of the full screen.


回答1:


You can set the layout constraints like you did, but you can also reference them with an IBOutlet and just change them programmatically (and call layoutIfNeeded after). It is very difficult to analyze your code without seeing the layout, but your Log shows that the constraints are not enough precise (ambiguous). To know which view is which, you can log the views individually as well and get the missing constraint for each view. Then one by one set the constraints so that it is not ambiguous anymore.

in your declaration:

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *leftMarginViewXYZ;

then in Interface Builder you right click on the constraint and draw the reference to your ViewController. When releasing the button you select the right constraint.

In your code you can then change the constraint with

self.leftMarginViewXYZ.constant = 123;
[self.view layoutIfNeeded];


来源:https://stackoverflow.com/questions/27473810/how-to-change-layout-constraints-programmatically-on-display-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!