我正在尝试按下按钮时向右移动文本字段。它工作正常,但是当我单击另一个文本字段或任何其他元素时,文本字段将重新显示在其当前位置。我浏览了互联网并发现它在我没有启用自动布局时有效,但我需要自动布局来设置变量位置。有什么建议?这是移动框的方法:
- (IBAction)EnterInfo:(id)sender {
CGRect newFrame = _UsernameField.frame;
newFrame.origin.x += 500; // shift right by 500pts
[UIView animateWithDuration:1.0
animations:^{
_UsernameField.frame = newFrame;
}];
}
上一个问题的新代码:
- (IBAction)EnterInfo:(id)sender {
[UIView animateWithDuration:1.0
animations:^{
_usernameX.constant = 130;
}];
}
答案 0 :(得分:2)
如果您启用了自动布局,则无法像这样操纵画面。您需要引用NSLayoutConstraint
并更新常量。
myXConstraint.constant = originalX + 500;
编辑 - 然后您的动画块应如下所示:
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];