Reputation: 397
I added 1 view in XIB, it is set autolayout. But when run, i want to to change frame of that view. I know if using autolayout, need to set translatesAutoresizingMaskIntoConstraints = YES
in my code. I have done that, I create new frame for that view but it shows the warning message in console:
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30] (Names: '|':UIView:0x16ee06b0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16e19e30 h=-&- v=-&- UIView:0x16ed3c30.midY == UIView:0x16ee06b0.midY + 44>",
"<NSAutoresizingMaskLayoutConstraint:0x16e19e60 h=-&- v=-&- UIView:0x16ed3c30.height == UIView:0x16ee06b0.height>"
)
Will attempt to recover by breaking constraint
<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30] (Names: '|':UIView:0x16ee06b0 )>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
This is the code for create new frame, i just want the origin of Y is equal to 0.
self.leftView.frame= CGRectMake(0, 0, 40, self.view.frame.size.height);
How can i fix that bug? Please give some advice. Thanks much.
Upvotes: 0
Views: 2853
Reputation: 11607
If I understand you correctly, I think you need to use a stored NSLayoutConstraint variable in your class, then update the constraint's constant value to 0 to make your UIView slide to the top.
Also, when you're using autolayout, you usually set:
myView.translateAutoResizingMaskIntoConstraint = NO;
That's NO instead of YES.
So an example would be something like:
@interface MyViewController()
{
NSLayoutConstraint *myViewTopConstraint;
}
@end
@implementation MyViewController
-(void)setupMyConstraintsMethod
{
...
// ---------------------------------------------------------------
// This constraint here says "myView" top edge should be pinned
// to the top edge of the view controller's view but with an offset
// of 50 pixels.
// ---------------------------------------------------------------
NSLayoutConstraint *myViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];
[self.view addConstraint:myViewTopConstraint];
}
-(void)buttonPressed:(id)sender
{
// ------------------------------------------------------------
// now you want myView's top edge to be at the top of your
// view controller's view, so you set the constant to 0
// ------------------------------------------------------------
myViewTopConstraint.constant = 0;
// animates the sliding up of "myView"
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
@end
Upvotes: 1