iOS:自动布局:设置动画后锁定新位置
我尝试为UIView编写一个category函数,该函数将获得将在其superview中固定给定帧的约束。我是这样实施的:iOS:自动布局:设置动画后锁定新位置,ios,objective-c,cocoa-touch,autolayout,nslayoutconstraint,Ios,Objective C,Cocoa Touch,Autolayout,Nslayoutconstraint,我尝试为UIView编写一个category函数,该函数将获得将在其superview中固定给定帧的约束。我是这样实施的: -(NSArray *)constraintsForLockingPositionInSuperview { NSLayoutConstraint *left=[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:N
-(NSArray *)constraintsForLockingPositionInSuperview
{
NSLayoutConstraint *left=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.superview
attribute:NSLayoutAttributeLeft multiplier:0 constant:self.frame.origin.x];
NSLayoutConstraint *height=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:Nil
attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:self.frame.size.height];
NSLayoutConstraint *width=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:Nil
attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:self.frame.size.width];
NSLayoutConstraint *top=[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.superview
attribute:NSLayoutAttributeTop multiplier:0 constant:self.frame.origin.y];
return @[left,height,width,top ];
}
然后在为视图设置动画后,通过在包含相关视图的视图控制器中执行以下操作来应用新约束。我删除视图,然后重新添加视图以删除视图上的约束,然后重新应用新约束。希望如果我添加另一个子视图,或者如果像actionView这样的东西出现,并且视图必须自行布局,那么这将保持视图的位置。高度和宽度似乎已正确锁定,但视图正在跳到中间,而不是锁定到位:
NSArray *lockingConstraints = [someView constraintsForLockingPositionInSuperview];
[someView removeFromSuperview];
[self.view addSubview:someView;
[self.view addConstraints:lockingConstraints];
[self.view layoutSubviews];
从以下文件中: 您不应该直接调用此方法
请打电话。谢谢你的指点。绝对不会再直接打电话了。但是,layoutIfNeeded也不起作用。不幸的是,它起作用了!我忘了我在动画中的主播点发生了变化。当我摆脱了那句话,它成功了!知道为什么会这样吗?