Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A couple of observations:</p> <ol> <li><p>Your constraint references a <code>toItem</code> of <code>self.view.superview</code>. I assume you meant <code>self.view</code>.</p></li> <li><p>You're adding the constraint to <code>_preView</code>, but you should add it to <code>self.view</code> (if you make the above change; if not, you'd use <code>self.view.superview</code>). You always add the constraint to the nearest shared parent.</p></li> <li><p>For the views you're creating programmatically, make sure to set <code>translatesAutoresizingMaskIntoConstraints</code> to <code>NO</code>.</p> <p>Thus:</p> <pre><code>_preView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_preView]; NSLayoutConstraint *myConstraint = [NSLayoutConstraint constraintWithItem:_preView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-239]; [self.view addConstraint:myConstraint]; </code></pre></li> </ol> <hr> <p>Chatting to you offline, two final observations:</p> <ol> <li><p>Your constraints were ambiguous. In the future, you can identify that by running the app in your debugger, hitting the pause button while the app is running (<img src="https://i.stack.imgur.com/MZmky.png" alt="enter image description here">) and then at the <code>(lldb)</code> prompt, you can enter</p> <p><code>po [[UIWindow keyWindow] _autolayoutTrace]</code></p> <p><img src="https://i.stack.imgur.com/HrfIE.png" alt="ambiguous layout"></p> <p>If you see <code>AMBIGUOUS LAYOUT</code>, then your constraints are not fully qualified (and thus you'll get unpredictable behavior). If you add the missing constraints, you should be able to eliminate this warning.</p></li> <li><p>If you want to animate constraint based views, you animate the changing of <code>constant</code> properties of the <code>constraints</code>, not by changing <code>frame</code> properties yourself. For example:</p> <pre><code> // create subview UIView *subview = [[UIView alloc] init]; subview.backgroundColor = [UIColor lightGrayColor]; subview.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:subview]; // create dictionary for VFL commands NSDictionary *views = @{@"subview" : subview, @"superview" : self.view}; // add horizontal constraints [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview]|" options:0 metrics:nil views:views]]; // set the height of the offscreen subview to be the same as its superview [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subview(==superview)]" options:0 metrics:nil views:views]]; // set the location of the subview to be just off screen below the current view NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.view.bounds.size.height]; [self.view addConstraint:constraint]; // then in two seconds, animate this subview back on-screen (i.e. change the top constraint `constant` to zero) double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ constraint.constant = 0.0; [UIView animateWithDuration:1.0 animations:^{ [self.view layoutIfNeeded]; }]; }); </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload