Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用autolayout在控制器中移动UIView_Ios_Objective C_Iphone_Autolayout_Ios Autolayout - Fatal编程技术网

Ios 如何使用autolayout在控制器中移动UIView

Ios 如何使用autolayout在控制器中移动UIView,ios,objective-c,iphone,autolayout,ios-autolayout,Ios,Objective C,Iphone,Autolayout,Ios Autolayout,我正在将一个应用程序更新到iOS7/8,并应用自动布局功能。 在myUIViewController中,我有几个UIView视图,它们的位置应该修改为UIScrollView更新其contentOffset 当我更新UIView的框架(旧方法)时,由于控制器自动布局约束(我使用VFL编程设置约束,我没有故事板),我会遇到一些小故障和错误 所以问题是:在实现自动布局的控制器中移动UIViews的正确/最佳方式是什么? 也许是更新约束?我是否应该使用核心动画进行任何UIView移动 提前谢谢 sel

我正在将一个应用程序更新到iOS7/8,并应用自动布局功能。 在my
UIViewController
中,我有几个
UIView
视图,它们的位置应该修改为
UIScrollView
更新其
contentOffset

当我更新
UIView的
框架(旧方法)时,由于控制器自动布局约束(我使用VFL编程设置约束,我没有故事板),我会遇到一些小故障和错误

所以问题是:在实现自动布局的控制器中移动
UIViews
的正确/最佳方式是什么? 也许是更新约束?我是否应该使用核心动画进行任何
UIView
移动

提前谢谢

self.yourView.translatesAutoresizingMaskIntoConstraints = YES;
然后只需更改帧:

CGRect rect = self.yourView.frame;
// move or change size
rect.origin.x = whatever...
self.yourView.frame = rect.

如果在故事板中设置了约束,则可以为需要更新的任何约束设置出口(就像对任何视图所做的那样)。 完成此操作后,可以根据需要更新constraint constant属性

[self layoutIfNeeded];
topConstraint.constant += 200;
[UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }]; 

在上面的代码中,我正在更新视图的topConstraint并设置更改的动画。您需要调用
[self layoutifneed]在修改约束之前一次,然后在动画块中再次执行。

如果使用视觉格式语言设置约束,我应该怎么做?(我没有故事板)。使用VFL有点困难,您应该尝试使用
+constraintswithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
来更新需要更新的约束。
+constraintsWithVisualFormat:options:metrics:views:
返回一个约束数组,你必须循环找到你需要的,然后保存它,这不是一个很好的主意如果我不想使用动画,我应该使用这个吗?:
[self layoutifneed];topConstraint.constant+=200;[需要自行布局]topConstraint.constant+=200;[需要自行布局]